我有一个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();
它可以工作,但一旦我添加了一个需要多个参数的新方法,当代理创建时,我开始收到这个异常(甚至发生任何通信之前):
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.
添加WebInvokeAttribute并将BodyStyle设置为wrap不起作用:
[OperationContract] [WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped)] bool setDeviceState(byte nodeId,bool powered,byte level);
应该注意的是,我有其他的方法可以工作,但它们只有一个参数,所以他们没有上述问题.
只是在FYI,我是如何设置主机:
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();
任何帮助是赞赏.
谢谢!