Wil*_*ode 15 c# rest wcf webget
我有一个RESTful WCF Web服务,其中包含以下API:
[WebGet(ResponseFormat = WebMessageFormat.Json)]
MyResponseContract GetFileInfo();
Run Code Online (Sandbox Code Playgroud)
尝试命中端点(使用SOAPUI)时,我看到以下错误消息:
服务器遇到处理请求的错误.请参阅服务帮助页面以构建对服务的有效请求.
我有SOAPUI设置为使用GET方法调用命中它.当我将其切换到没有正文的POST时,它会失败并显示以下消息:
方法不允许.
这很有道理:无法通过POST获得GET.所以我更新了我的代码如下:
[WebInvoke(ResponseFormat = WebMessageFormat.Json)]
MyResponseContract GetFileInfo();
Run Code Online (Sandbox Code Playgroud)
现在我使用POST方法从SOAPUI调用它并且它可以工作.好奇.所以我现在更改我的代码如下:
[WebInvoke(ResponseFormat = WebMessageFormat.Json, Method = "GET")]
MyResponseContract GetFileInfo();
Run Code Online (Sandbox Code Playgroud)
我在一些帖子中看到,这基本上等同于一个WebGet
属性.这也行不通.
所以我的问题是:WebGet
即使我不接受参数或使用自定义UriTemplate,为什么这不起作用?
我试图点击它(它在IIS本地托管)的Url是:
http://localhost/Utilities/API/GetFileInfo
更新 鉴于下面的评论和给出的答案,我仍然面临这个问题.一些额外的细节.
我的web层web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<customErrors mode="Off" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" maxReceivedMessageSize="10000000" />
</webHttpEndpoint>
</standardEndpoints>
<behaviors>
<endpointBehaviors>
<behavior name="exampleBehavior">
<callbackDebug includeExceptionDetailInFaults="true" />
<enableWebScript />
<webHttp helpEnabled="true" />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="WebHttpBinding" maxReceivedMessageSize="10000000" />
</webHttpBinding>
</bindings>
<client>
<endpoint address="http://LOCALHOST/Utilities.AppService/API"
binding="webHttpBinding" bindingConfiguration="WebHttpBinding"
contract="Utilities.Common.API.IMyApi"
behaviorConfiguration="exampleBehavior" />
</client>
</system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我的app-layer web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<customErrors mode="Off" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" maxReceivedMessageSize="10000000" />
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我的服务界面
[ServiceContract(Namespace = "API")]
public interface IMyApi
{
[WebGet]
MyResponseContract GetFileInfo();
}
Run Code Online (Sandbox Code Playgroud)
我的网络层实现
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class MyApiWebService : ClientBase<IMyApi>, IMyApi
{
public MyResponseContract GetFileInfo()
{
return Channel.GetFileInfo();
}
}
Run Code Online (Sandbox Code Playgroud)
我的app层实现
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class MyApiAppService : IMyApi
{
public MyResponseContract GetFileInfo()
{
return new MyResponseContract();
}
}
Run Code Online (Sandbox Code Playgroud)
我的网页层Global.asax:
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("API", new WebServiceHostFactory(), typeof(MyApiWebService)));
}
Run Code Online (Sandbox Code Playgroud)
我的应用层Global.asax:
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("API", new WebServiceHostFactory(), typeof(MyApiAppService)));
}
Run Code Online (Sandbox Code Playgroud)
我不确定我能提供多少细节.正如您所看到的,鉴于提供的解决方案,我已经实施了一切无效的建议.无论我是尝试WebGet
通过将Web图层服务URL放在浏览器中,还是使用SOAPUI,或尝试使用服务客户端使用C#单元测试来命中它,我都无法使用WebGet
.再次感谢您的帮助.
有趣的是,应用层URL工作正常.但是网络层没有.所以:
localhost/Utilities.AppService/API/GetFileInfo
工作,而
localhost/Utilities.WebService/API/GetFileInfo
才不是.
所以在我最近更新之前这可能不是很明显,但我有两个RESTful服务,它们彼此通信但是生活在不同的域中.Web层服务是第一个联系点,App层服务是实际的工作实践者.在这种情况下,我能够进一步调试,发现从Web到App层的调用实际异常是405(Method Not Allowed).我经过多次挖掘后发现这个链接解决了我的问题.
当ClientBase<>
您使用服务之间的通信方法时,您基本上需要重新建立调用之间的操作上下文.否则一切都变成了POST,因此只有POST工作.
我希望这有助于其他人,我非常感谢大家帮忙调试这个.
为了演示这看起来是什么,以下是我更新的,有效的Web服务实现:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class MyApiWebService : ClientBase<IMyApi>, IMyApi
{
public MyResponseContract GetFileInfo()
{
MyResponseContract output = null;
using(var context = new OperationContext(Channel as IContextChannel))
{
output = Channel.GetFileInfo();
}
return output;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1120 次 |
最近记录: |