bug*_*ixr 9 c# asp.net wcf c#-4.0
我有一个WebServiceHost,用于在控制台应用程序中托管一些Web服务.我在我的客户端应用程序中添加了一个服务引用并创建代理,如下所示:
var binding = new WebHttpBinding();
var endPoint = new EndpointAddress(string.Format(Settings.serviceBase, Settings.wcfPort));
ChannelFactory<IzWaveSVC> factory = new ChannelFactory<IzWaveSVC>(new WebHttpBinding(), endPoint);
factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
// **Exception occurs here**
var proxy = (IzWaveSVC)factory.CreateChannel();
Run Code Online (Sandbox Code Playgroud)
它可以工作,但是一旦我添加了一个需要多个参数的新方法,我就会在创建代理时开始获取此异常(这是在任何通信发生之前):
Operation 'setDeviceState' of contract 'IzWaveSVC' specifies multiple request
body parameters to be serialized without any wrapper elements. At most one
body parameter can be serialized without wrapper elements. Either remove the
extra body parameters or set the BodyStyle property on the WebGetAttribute /
WebInvokeAttribute to Wrapped.
Run Code Online (Sandbox Code Playgroud)
添加WebInvokeAttribute并将BodyStyle设置为wrap包含无效:
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped)]
bool setDeviceState(byte nodeId, bool powered, byte level);
Run Code Online (Sandbox Code Playgroud)
应该注意的是,我有其他方法可行,但它们只有一个参数,因此它们没有上述问题.
仅供参考,以下是我设置主机的方法:
endPoint = new EndpointAddress(string.Format(Settings.serviceBase, port));
binding = new WebHttpBinding();
host = new WebServiceHost(singletonObject, new Uri(string.Format(Settings.serviceBase, port)));
host.AddServiceEndpoint(typeof(IzWaveSVC), binding, "");
ServiceMetadataBehavior mexBehavior = new ServiceMetadataBehavior();
mexBehavior.HttpGetEnabled = true;
host.Description.Behaviors.Add(mexBehavior);
host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), endPoint.Uri.AbsoluteUri + "mex");
host.Open();
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏.
谢谢!
ami*_*mit 15
您似乎已使用VS中的"添加服务引用"对话框创建了代理代码.VS ASR对话框不完全支持WCF REST,因此代理代码缺少[WebInvoke]
属性.您可以尝试[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped)]
在客户端代理中添加操作属性吗?
我发现两个解决方案:
如果你可以删除 <webHttp/>
从
<behaviors>
<endpointBehaviors>
<behavior>
<!--<webHttp/>-->
</behavior>
</endpointBehaviors>
</behaviors>
Run Code Online (Sandbox Code Playgroud)
如果你不能,我不得不添加属性
[WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest)]
Run Code Online (Sandbox Code Playgroud)
在操作联系方法之上
归档时间: |
|
查看次数: |
22893 次 |
最近记录: |