Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 窗口应用程序与网站WCF web服务通信_C#_Wcf_.net 4.0_Windows Applications - Fatal编程技术网

C# 窗口应用程序与网站WCF web服务通信

C# 窗口应用程序与网站WCF web服务通信,c#,wcf,.net-4.0,windows-applications,C#,Wcf,.net 4.0,Windows Applications,我是新手,在实现与windows应用程序和网站WCF web服务的通信时遇到问题。它一直在显示错误,我已经尝试过很多次了。希望有人能帮助我~ 网站 IApiService.cs [ServiceContract] public interface IApiService { [OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped,

我是新手,在实现与windows应用程序和网站WCF web服务的通信时遇到问题。它一直在显示错误,我已经尝试过很多次了。希望有人能帮助我~

网站 IApiService.cs

[ServiceContract]
public interface IApiService
{
    [OperationContract]
    [WebInvoke(Method = "POST",
          BodyStyle = WebMessageBodyStyle.Wrapped,
          ResponseFormat = WebMessageFormat.Json)]
    Boolean TestConnection();

    [OperationContract]  
    [WebInvoke(Method = "POST",
          BodyStyle = WebMessageBodyStyle.Wrapped,
          ResponseFormat = WebMessageFormat.Json)]
    Boolean IsPhoneNumberListMatch(int pa, int pb);

    [OperationContract]
    [WebInvoke(Method = "POST",
          BodyStyle = WebMessageBodyStyle.Wrapped,
          ResponseFormat = WebMessageFormat.Json)]
    List<ClientPhoneModel> GetAllPhoneNumber(string pa);

    [OperationContract]
    [WebInvoke(Method = "POST",
          BodyStyle = WebMessageBodyStyle.Wrapped,
          ResponseFormat = WebMessageFormat.Json)]
    List<ClientRegistrationModel> GetAllNewRegistration(string pa);
}
try {
    Localhost.ApiServiceClient client = new Localhost.ApiServiceClient();
    client.TestConnection();
}
catch(Exception ex) {
    Log.Exception(ex);
}
App.config

<system.serviceModel>
    <behaviors>
        <endpointBehaviors>
        <behavior name="EndBehavior">
            <webHttp />
        </behavior>
    </endpointBehaviors>
    <serviceBehaviors>
        <behavior name="ServiceBehavior">
            <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
            <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
    </serviceBehaviors>
    </behaviors>
    <services>
        <service behaviorConfiguration="ServiceBehavior" name="ApiService">
            <endpoint address="basic" binding="webHttpBinding" contract="IApiService" behaviorConfiguration="EndBehavior"/>
        </service>
        <service behaviorConfiguration="ServiceBehavior" name="WebService">
            <endpoint address="" binding="webHttpBinding" contract="IWebService" behaviorConfiguration="EndBehavior"/>
        </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
                <behavior name="ServiceBehavior">
                    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="true"/>
                </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="EndBehavior">
                <webHttp/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <client>
        <endpoint address="http://localhost:5151/ApiService.svc" behaviorConfiguration="EndBehavior" binding="webHttpBinding" contract="Localhost.IApiService" name="ApiServiceClient" />
    </client>
</system.serviceModel>

我能得到的最新错误是

Message : 
    Operation 'IsPhoneNumberListMatch' of contract 'IApiService' 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.
Stack trace :
    at System.ServiceModel.Description.WebHttpBehavior.TryGetNonMessageParameterType(MessageDescription message, OperationDescription declaringOperation, Boolean isRequest, Type& type)
    at System.ServiceModel.Description.WebHttpBehavior.ValidateBodyStyle(OperationDescription operation, Boolean request)
    at System.ServiceModel.Description.WebHttpBehavior.<>c__DisplayClass7.<>c__DisplayClassa.<GetRequestClientFormatter>b__4()
    at System.ServiceModel.Description.WebHttpBehavior.<>c__DisplayClass7.<GetRequestClientFormatter>b__3()
    at System.ServiceModel.Description.WebHttpBehavior.HideReplyMessage(OperationDescription operationDescription, Effect effect)
    at System.ServiceModel.Description.WebHttpBehavior.GetRequestClientFormatter(OperationDescription operationDescription, ServiceEndpoint endpoint)
    at System.ServiceModel.Description.WebHttpBehavior.ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    at System.ServiceModel.Description.DispatcherBuilder.ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime)
    at System.ServiceModel.Description.DispatcherBuilder.BuildProxyBehavior(ServiceEndpoint serviceEndpoint, BindingParameterCollection& parameters)
    at System.ServiceModel.Channels.ServiceChannelFactory.BuildChannelFactory(ServiceEndpoint serviceEndpoint, Boolean useActiveAutoClose)
    at System.ServiceModel.ChannelFactory.CreateFactory()
    at System.ServiceModel.ChannelFactory.OnOpening()
    at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
    at System.ServiceModel.ChannelFactory.EnsureOpened()
    at System.ServiceModel.ChannelFactory`1.CreateChannel(EndpointAddress address, Uri via)
    at System.ServiceModel.ChannelFactory`1.CreateChannel()
    at System.ServiceModel.ClientBase`1.CreateChannel()
    at System.ServiceModel.ClientBase`1.CreateChannelInternal()
    at System.ServiceModel.ClientBase`1.get_Channel()
    at LoonTeleShopClient.Localhost.ApiServiceClient.TestConnection() in c:\Users\Kelvin\Documents\Visual Studio 2012\Projects\LoonTeleShopClient\LoonTeleShopClient\Service References\Localhost\Reference.cs:line 339
    at LoonTeleShopClient.Index..ctor() in c:\Users\Kelvin\Documents\Visual Studio 2012\Projects\LoonTeleShopClient\LoonTeleShopClient\Index.cs:line 44
消息:
合约“IApiService”的操作“IsPhoneNumberListMatch”指定要序列化的多个请求正文参数,而不使用任何包装器元素。在没有包装器元素的情况下,最多可以序列化一个主体参数。删除额外的主体参数或将WebGetAttribute/WebInvokeAttribute上的BodyStyle属性设置为Wrapped。
堆栈跟踪:
位于System.ServiceModel.Description.WebHttpBehavior.TryGetNonMessageParameterType(MessageDescription message,OperationDescription declaringOperation,Boolean isRequest,Type&Type)
位于System.ServiceModel.Description.WebHttpBehavior.ValidateBodyStyle(OperationDescription操作,布尔请求)
在System.ServiceModel.Description.WebHttpBehavior.c_uuuDisplayClass7.c_uuuDisplayClassA.b_uuu4()中
在System.ServiceModel.Description.WebHttpBehavior.c__DisplayClass7.b__3()中
位于System.ServiceModel.Description.WebHttpBehavior.HideReplyMessage(操作说明操作说明效果)
位于System.ServiceModel.Description.WebHttpBehavior.GetRequestClientFormatter(OperationDescription OperationDescription,ServiceEndpoint)
位于System.ServiceModel.Description.WebHttpBehavior.ApplyClientBehavior(ServiceEndpoint端点,ClientRuntime客户端运行时)
位于System.ServiceModel.Description.DispatcherBuilder.ApplyClientBehavior(ServiceEndpoint ServiceEndpoint,ClientRuntime ClientRuntime)
位于System.ServiceModel.Description.DispatcherBuilder.BuildProxyBehavior(ServiceEndpoint ServiceEndpoint、BindingParameterCollection和parameters)
位于System.ServiceModel.Channels.ServiceChannelFactory.BuildChannelFactory(ServiceEndpoint ServiceEndpoint,布尔值useActiveAutoClose)
在System.ServiceModel.ChannelFactory.CreateFactory()中
在System.ServiceModel.ChannelFactory.OnOpen()中
在System.ServiceModel.Channels.CommunicationObject.Open处(TimeSpan超时)
在System.ServiceModel.ChannelFactory.Resulted()中
位于System.ServiceModel.ChannelFactory`1.CreateChannel(端点地址,Uri通过)
位于System.ServiceModel.ChannelFactory`1.CreateChannel()
位于System.ServiceModel.ClientBase`1.CreateChannel()
位于System.ServiceModel.ClientBase`1.CreateChannelInternal()
在System.ServiceModel.ClientBase`1.get_Channel()中
在c:\Users\Kelvin\Documents\Visual Studio 2012\Projects\LoonTeleShopClient\LoonTeleShopClient\Service References\Localhost\ApisServiceClient.TestConnection()中
在c:\Users\Kelvin\Documents\visualstudio 2012\Projects\LoonTeleShopClient\LoonTeleShopClient\Index.cs中的LoonTeleShopClient.Index..ctor()处:第44行

请帮帮我~~~~万分感谢

这可能不是您想要的答案,但我建议在必须提供非soap绑定时,不要在操作中使用多个参数。这样做的主要原因是,仅使用“裸”bodyStyle,您就可以获得一个很好的帮助页面。 尝试设置:

<behaviors>
  <endpointBehaviors>
    <behavior name="EndBehavior">
      <webHttp automaticFormatSelectionEnabled="true" defaultBodyStyle="Bare" helpEnabled="true"/>
    </behavior>
  </endpointBehaviors>
</behaviors>

这将:

  • 让用户决定是否使用json或xml进行通信
    (已启用自动格式选择)
  • 设置默认的BodyStyle
  • 启用“帮助”页面
  • 如果执行此操作,则可以删除所有WebInvoke属性: [WebInvoke(Method=“POST”,BodyStyle=WebMessageBodyStyle.Wrapped,ResponseFormat=WebMessageFormat.Json)]

    您可以在以下位置找到帮助页面: 因此,如果我在service.motor.car上有一个名为Engine.svc的服务,并且我的端点地址是“v1/web”,我会在

    希望这有帮助:-)

    致意
    db

    重复此Hi,我尝试过,但仍然给出相同的错误。正如您在我的代码中看到的,还有BodyStyle=Wrapped。