使用soap将字节数组传递给wcf

时间:2013-12-13 13:22:50

标签: asp.net-mvc-3 wcf c#-4.0 wcf-binding wcf-security

在这里,当我尝试发送字节数组时,它给了我错误的请求错误。我已检查所有邮件限制的最大值。全部都设置为最大值。 但是,我可以通过将字节数组参数保持为null来将字符串值发送到服务方法。我不知道为什么会发生这种情况。我还检查了服务行为配置名称和绑定配置名称(即包含的命名空间),这似乎很好。非常感谢任何帮助。

这是我的客户端代码:

....
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);

         }
    }
}
.......

客户端网络配置:

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

服务网站配置

<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 个答案:

答案 0 :(得分: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>