簡體   English   中英

窗口應用程序與網站WCF Web服務進行通信

[英]Window application communicate with website WCF web service

我是新手,在與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);
}

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>

窗口應用

添加了 localhost:5151 / ApiService.svc作為服務參考

Index.cs

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

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>

我能得到的最新錯誤是

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

請幫我~~~萬謝

這可能不是您想要的答案,但是我建議在必須提供非肥皂綁定時,在操作上不要有多個參數。 這樣做的主要原因是,僅通過使用“裸露” bodyStyle,您可以獲得一個不錯的幫助頁面。 嘗試設置:

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

這將:

  1. 讓用戶決定是否要使用json或xml進行通信
    (automaticFormatSelectionEnabled)
  2. 設置默認的BodyStyle
  3. 啟用幫助頁面

如果這樣做,則可以刪除所有WebInvoke屬性:[WebInvoke(方法=“ POST”,BodyStyle = WebMessageBodyStyle.Wrapped,ResponseFormat = WebMessageFormat.Json)]

您可以在以下位置找到幫助頁面: http://your.domain/servicename/endpointAddress/help因此,如果在service.motor.car上有一個名為Engine.svc的服務,並且我的端點地址是“ v1 / web”,在http://service.motor.car/Engine.svc/v1/web/help上找到我的幫助頁面

希望這可以幫助 :-)

最好的問候數據庫

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM