2010-09-05 93 views
2

我有一个silverlight应用程序,它与ASP.NET网站中的WCF服务对话。下面的代码工作:Silverlight WCF使用BasicHttpBinding,使用CustomBinding/BinaryMessageEncoding时出现“HTTP 415 Unsupported Media Type”异常

 var service = new ChannelFactory<IService>(new BasicHttpBinding() 
                    { 
                     MaxReceivedMessageSize = int.MaxValue 
                    }, 
                    new EndpointAddress(Settings.ServiceUrl)).CreateChannel(); 

但我真的想利用“二进制编码”。要使用二进制编码运行服务,您不能使用BasicHttpBinding,您需要使用CustomBinding!以下代码在相同位置使用,但会从Web服务器中产生HTTP 415 Unsupported媒体类型状态。在调试会话中,服务器上没有达到断点。

  var service = new ChannelFactory<IService>(new CustomBinding(new BinaryMessageEncodingBindingElement(), new HttpTransportBindingElement() 
                                   { 
                                    MaxReceivedMessageSize = int.MaxValue 
                                   }), 
                    new EndpointAddress(Settings.ServiceUrl)).CreateChannel(); 

我需要帮助找出为什么这个设置不起作用! BTW这里是我的web配置服务节在服务器端:

<system.serviceModel> 
<bindings> 
    <customBinding> 
    <binding name="myBinding"> 
     <binaryMessageEncoding /> 
     <httpTransport authenticationScheme="Negotiate"/> 
    </binding> 
    </customBinding> 
</bindings> 
<services> 
    <service name="myService"> 
    <endpoint address="" binding="customBinding" bindingConfiguration="myBinding" /> 
    </service> 
</services> 
<behaviors> 
    <serviceBehaviors> 
    <behavior name="wcfServiceBehavior"> 
     <serviceMetadata httpGetEnabled="true" /> 
     <serviceDebug includeExceptionDetailInFaults="true" /> 
     <dataContractSerializer maxItemsInObjectGraph="2147483647"/> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 

回答

3

我想出如何使BinaryMessageEncoding,我可以证实BinaryMessageEncoding正常工作在IIS和Visual Studio Web开发调试服务器。

问题在于太常见的WCF配置问题。我的“服务名称”是一个任意名称,但它应该是服务类的完全限定名称。 WCF不是抛出一个异常,而是以某种默认行为公开服务,并且不警告我该配置无效。

<bindings> 
<customBinding> 
     <binding name="myBinding" > 
      <binaryMessageEncoding > 
      <!--readerQuotas are used to set upper limits on message payload--> 
      <readerQuotas 
       maxDepth="2147483647" 
       maxStringContentLength="2147483647" 
       maxArrayLength="2147483647" 
       maxBytesPerRead="2147483647" 
       maxNameTableCharCount="2147483647" 
      /> 
      </binaryMessageEncoding> 
      <httpTransport authenticationScheme="Ntlm" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647"/> 
     </binding> 
     </customBinding> 
    </bindings> 
    <services> 
     <!--the service name must be the fully-qualified name of the service class--> 
     <service name="MyProgram.DataService"> 
     <!--this line allows metatada exchange--> 
     <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" /> 
     <endpoint address="" binding="customBinding" bindingConfiguration="myBinding" contract="MyProgram.IDataService" /> 
     </service> 
    </services> 
0

我不明白,所有的Silverlight的能力,但在现阶段,我认为你的服务不能在所有的工作。您的自定义绑定没有任何必需的传输绑定元素。

试图改变你的绑定:

<bindings> 
    <customBinding> 
    <binding name="myBinding"> 
     <binaryMessageEncoding /> 
     <httpTransport /> 
    </binding> 
    </customBinding> 
</bindings> 
+0

是的,谢谢你指出,我已经编辑了上面的代码。我再次使用web.config中的元素重试了它,并且问题完全没有改变,BasicHttpBinding有效,CustomBinding引发NotFound异常。 – nachonachoman 2010-09-07 01:26:24

+0

更新 - 感谢您的帮助。我能够调整本地URL并添加一个点“。”到地址,这使得提琴手能够监控本地流量。现在我可以看到真正的罪魁祸首是来自调试Web服务器的HTTP 415 Unsupported Media Type。 – nachonachoman 2010-10-10 23:51:57

+0

调试Web服务器?你的意思是卡西尼(包含在Visual Studio中的网络服务器)?我认为卡西尼不支持二进制编码。在开发高级WCF服务时,您应该使用IIS或自托管。 – 2010-10-11 07:16:55

相关问题