2013-12-13 29 views
0

在這裏,當我想發送字節數組它給我錯誤的請求錯誤。我已經檢查了所有消息限制的最大值。所有都設置爲最大值。 但是我能夠通過將字節數組參數保留爲空來將字符串值發送到service方法。我不知道爲什麼發生在這裏。我還檢查了服務行爲配置名稱和綁定配置名稱(即包含的名稱空間),這似乎很好。任何幫助都非常感謝。傳遞字節數組到wcf使用肥皂

這是我的客戶端代碼:

.... 
ServiceClient ac = new ServiceClient(); 
string ServiceUrl = "http://localhost:20314/Service.svc"; 
EndpointAddress address = new EndpointAddress(ServiceUrl); 
BasicHttpBinding httpbinding = new BasicHttpBinding(); 
using (ChannelFactory<IServiceChannel> factory = new ChannelFactory<IServiceChannel>(httpbinding)) 
{ 
    factory.Endpoint.Address = address; 
    using (IServiceChannel oService = factory.CreateChannel()) 
    { 

     using (OperationContextScope scope = new OperationContextScope(oService)) 
     { 

     Guid myToken = new Guid("guid here");      

     MessageHeader<Guid> mhg = new MessageHeader<Guid>(myToken);      
     MessageHeader untyped = mhg.GetUntypedHeader("identity", "ns"); 
     OperationContext.Current.OutgoingMessageHeaders.Add(untyped); 
     // here i am getting bad request error                
     var fdt = (oService).GetData(bytearray, value); 

     } 
    } 
} 
....... 

客戶端的web配置:

<system.web> 
<compilation debug="true" targetFramework="4.0"/>  
<httpRuntime maxRequestLength="500000000" executionTimeout="360"/> 
</system.web> 
<system.serviceModel> 
<bindings> 
    <basicHttpBinding>   
    <binding name="BasicHttpBinding_IService" closeTimeout="01:01:00" 
     openTimeout="01:01:00" receiveTimeout="01:10:00" sendTimeout="01:01:00" 
     allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
     maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" 
     messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
     useDefaultWebProxy="true"> 
     <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" 
     maxBytesPerRead="4096" maxNameTableCharCount="2147483647" /> 
     <security mode="None"> 
     <transport clientCredentialType="None" proxyCredentialType="None" 
      realm="" /> 
     <message clientCredentialType="UserName" algorithmSuite="Default" /> 
     </security> 
    </binding> 
    </basicHttpBinding> 
</bindings> 

<client>  
    <endpoint address="http://localhost:20314/Service.svc" binding="basicHttpBinding" 
    bindingConfiguration="BasicHttpBinding_IService" contract="LocalService.IService" 
    name="BasicHttpBinding_IService"/> 
</client> 

<services>  
    <service name="LocalService.Service" behaviorConfiguration="ServiceBehavior"> 
    <endpoint address="http://localhost:20314/Service.svc" binding="wsHttpBinding" contract="LocalService.IService" /> 
    </service> 
</services> 

<behaviors> 
    <serviceBehaviors>   
    <behavior name="ServiceBehavior"> 
     <serviceMetadata httpGetEnabled="true"/> 
     <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 

服務Web配置

<system.web> 
<compilation debug="true" targetFramework="4.0" /> 
<httpRuntime maxRequestLength="500000000" executionTimeout="360"/> 
</system.web> 
<system.webServer> 
<httpErrors errorMode="Detailed" /> 
<asp scriptErrorSentToBrowser="true" /> 
</system.webServer> 
<system.serviceModel>  
<behaviors> 
    <serviceBehaviors> 
    <behavior>   
     <serviceAuthorization serviceAuthorizationManagerType="ServiceApp.APIKeyAuthorization, ServiceApp" /> 
     <serviceMetadata httpGetEnabled="true" /> 
     <dataContractSerializer maxItemsInObjectGraph="2147483647" /> 
     <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 
<standardEndpoints> 
    <webHttpEndpoint> 
    <standardEndpoint 
     automaticFormatSelectionEnabled="true" 
     helpEnabled="true" /> 
    </webHttpEndpoint> 
</standardEndpoints> 
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="false"/> 
</system.serviceModel> 
<system.webServer> 
<modules runAllManagedModulesForAllRequests="true"/> 
</system.webServer> 

回答

1

我在配置文件中看到了幾件事情。

首先,在您的客戶端配置中,您有一個<services>部分 - 除非您的客戶端也託管服務,您不需要那裏。此外,您的<services>部分使用wsHttpBinding,但您的客戶端正在指定basicHttpBinding - 客戶端和服務綁定需要匹配。

其次,您爲客戶端的綁定設置了較高的值,但客戶端綁定配置對服務器端沒有影響。您的服務配置中沒有定義明確的端點,因此您將獲得具有默認綁定的默認端點(basicHttpBinding),該默認端點具有正常的默認值。

您可以在您的服務配置增加對basicHttpBinding默認配置(通過省略name屬性)是這樣的:

<bindings> 
    <basicHttpBinding>   
    <binding closeTimeout="01:01:00" 
      openTimeout="01:01:00" receiveTimeout="01:10:00" sendTimeout="01:01:00" 
      allowCookies="false" bypassProxyOnLocal="false" 
      hostNameComparisonMode="StrongWildcard" 
      maxBufferSize="2147483647" maxBufferPoolSize="2147483647" 
      maxReceivedMessageSize="2147483647" 
      messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
      useDefaultWebProxy="true"> 
     <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" 
        maxArrayLength="2147483647" 
        maxBytesPerRead="4096" maxNameTableCharCount="2147483647" /> 
     <security mode="None"> 
     <transport clientCredentialType="None" proxyCredentialType="None" 
        realm="" /> 
     <message clientCredentialType="UserName" algorithmSuite="Default" /> 
     </security> 
    </binding> 
    </basicHttpBinding> 
</bindings> 

這是從你的client.config相同的定義,減去name屬性。 (另外,爲什麼您定義了傳輸和消息安全性,但安全模式設置爲「無」?)。

另一種選擇是在服務配置中顯式定義一個端點並指定要使用的綁定配置。與你在客戶端配置文件中做的一樣。假設您有一個名爲「BasicHttpBinding_IService」的basicHttpBinding。你的終點會是這個樣子:

<services>  
    <service name="LocalService.Service" behaviorConfiguration="ServiceBehavior"> 
    <endpoint address="http://localhost:20314/Service.svc" 
       binding="basicHttpBinding" 
       bindingConfiguration="BasicHttpBinding_IService" 
       contract="LocalService.IService" /> 
    </service> 
</services> 
+0

你救了我的時間 –

+0

我abopted第一種方法,而不是在服務配置方面增加服務,因爲它給我同樣的錯誤。 –

+0

任何感謝你的第一種方法,它工作得很好。 –