简体   繁体   English

窗口应用程序与网站WCF Web服务进行通信

[英]Window application communicate with website WCF web service

I'm new and having problem to implement communication with windows application and website WCF web service. 我是新手,在与Windows应用程序和网站WCF Web服务实现通信时遇到问题。 It's keep on showing error and I've tried many times. 它一直显示错误,我已经尝试了很多次。 Hope someone can please help me~~ 希望有人可以帮助我~~

Website 网站

IApiService.cs 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);
}

web.config web.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>

Window Application 窗口应用

Added localhost:5151/ApiService.svc as Service Reference 添加了 localhost:5151 / ApiService.svc作为服务参考

Index.cs Index.cs

try {
    Localhost.ApiServiceClient client = new Localhost.ApiServiceClient();
    client.TestConnection();
}
catch(Exception ex) {
    Log.Exception(ex);
}

App.config App.config

<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>

The latest error I could get is 我能得到的最新错误是

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

Please help me~~~ million thanks 请帮我~~~万谢

This may not be the answer you wanted, but I suggest not having multiple parameters on your operations when having to provide a non-soap binding. 这可能不是您想要的答案,但是我建议在必须提供非肥皂绑定时,在操作上不要有多个参数。 The primary reason for this is that by only using the "Bare" bodyStyle, you can get a nice help-page. 这样做的主要原因是,仅通过使用“裸露” bodyStyle,您可以获得一个不错的帮助页面。 Try setting: 尝试设置:

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

This will: 这将:

  1. Let the user decide if they want to communicate using json or xml 让用户决定是否要使用json或xml进行通信
    (automaticFormatSelectionEnabled) (automaticFormatSelectionEnabled)
  2. Set a default BodyStyle 设置默认的BodyStyle
  3. Enable the help page 启用帮助页面

If you do this, you can delete all your WebInvoke attributes: [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)] 如果这样做,则可以删除所有WebInvoke属性:[WebInvoke(方法=“ POST”,BodyStyle = WebMessageBodyStyle.Wrapped,ResponseFormat = WebMessageFormat.Json)]

You can find the help-page at: http://your.domain/servicename/endpointAddress/help So if I have a service called Engine.svc at service.motor.car and my endpoint address is "v1/web" I would find my help-page at http://service.motor.car/Engine.svc/v1/web/help 您可以在以下位置找到帮助页面: http://your.domain/servicename/endpointAddress/help因此,如果在service.motor.car上有一个名为Engine.svc的服务,并且我的端点地址是“ v1 / web”,在http://service.motor.car/Engine.svc/v1/web/help上找到我的帮助页面

Hope this helps :-) 希望这可以帮助 :-)

Best regards db 最好的问候数据库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM