使用mtom流式传输二进制数据的wcf webservice获取错误“接收HTTP响应时发生错误***”

时间:2012-11-26 23:58:10

标签: wcf web-services serialization mtom

我必须开发一个能够使用wcf和mtom进行序列化的附件和客户端发送电子邮件的Web服务。我使用以下工具进行工作:

Windows 7 Professional x64 Microsoft Visual Studio 2012 Microsoft Development Server / IIS Express 8.0 用户:管理员

每当我尝试使用来自邮件合同的自定义类在我的客户端中使用web服务时,我收到错误"收到对http://localhost:2364/Service1.svc的HTTP响应时发生错误。这可能是由于服务端点绑定不使用HTTP协议。这也可能是由于服务器中止HTTP请求上下文(可能是由于服务关闭)。有关详细信息,请参阅服务器日志。

现在,我只是尝试设置一个简单的测试服务和一个简单的测试客户端。我尝试了以下方法,并且总是得到相同的错误,同时使用webservice中的方法,它只是在请求中使用简单的字符串,响应很好,没有任何错误。

  1. 从Visual Studio 2012和IIS Express 8.0集成的Microsoft Development Server上出现相同的错误

  2. 在客户端和服务的配置中使用basicHttpBinding或wsHttpBinding时出现相同的错误

  3. 试图在两台服务器上生成日志文件。 IIS Express Server在配置的其他文件夹中生成一些日志,但不包含配置文件中的设置。 Microsoft Development Server不生成任何内容。

  4. 我使用svctraceviewer.exe来查看少数生成的日志记录文件,只是看到一些警告,其中包含操作码MODULE_SET_RESPONSE_ERROR_STATUS。我无法理解这些信息。

  5. 我尝试将localService和networkService的读/写权限启用到指定的文件夹而没有结果。

  6. 我尝试使用RawCap来嗅探本地主机并使用wireshark观察结果,但输出文件有0个字节(这一定是另一个问题)。

  7. 如果您需要更多信息,请与我们联系。

    谢谢!

    以下是我的服务和客户端的代码和配置(仅用于测试):

    服务:IService1.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.Text;
    using System.IO;
    
    namespace WcfTestService
    {
    
    [ServiceContract]
    public interface IService1
    {
    
        [OperationContract]
        string GetData(int value);
    
        /*
        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);*/
    
        // TODO: Hier Dienstvorgänge hinzufügen
    
        [OperationContract]
        ServiceResponse TestCase(ServiceRequest testRequest);
    }
    
    /*
    // Verwenden Sie einen Datenvertrag, wie im folgenden Beispiel dargestellt, um Dienstvorgängen zusammengesetzte Typen hinzuzufügen.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";
    
        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }
    
        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }*/
    
    [MessageContract]
    public class ServiceRequest
    {
        [MessageHeader]
        public String Type { get; set; }
    
        [MessageBodyMember]
        public Stream Contents { get; set; }
    }
    
    [MessageContract]
    public class ServiceResponse
    {
        [MessageHeader]
        public String Type { get; set; }
    
        [MessageBodyMember]
        public Stream Contents { get; set; }
    }
    }
    

    服务:web.config:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    
    <system.diagnostics>
    <sources>
      <source name="System.ServiceModel.MessageLogging"  switchValue="Ausführlich,ActivityTracing">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="ServiceModelMessageLoggingListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
      <source propagateActivity="true" name="System.ServiceModel" switchValue="Ausführlich,ActivityTracing">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="ServiceModelTraceListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add initializeData="d:\log\web_messages.svclog"
        type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
        <filter type="" />
      </add>
      <add initializeData="d:\log\web_tracelog.svclog"
        type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
        <filter type="" />
      </add>
    </sharedListeners>
    <trace autoflush="true" />
    </system.diagnostics>
    <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    </appSettings>
    <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <!--httpRuntime targetFramework="4.5"/>-->
    <httpRuntime targetFramework="4.5" maxRequestLength="102400" executionTimeout="7200"/>
    </system.web>
    <system.serviceModel>
    <diagnostics>
      <messageLogging logEntireMessage="true" logKnownPii="true" logMalformedMessages="true"
        logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true" />
      <endToEndTracing propagateActivity="true" activityTracing="true"
        messageFlowTracing="true" />
    </diagnostics>
    <bindings>
      <basicHttpBinding>
        <binding name="basicHttpMtomBinding" closeTimeout="10:01:00"
          openTimeout="10:01:00" receiveTimeout="10:10:00" sendTimeout="10:01:00"
          transferMode="Streamed" messageEncoding="Mtom">
          <readerQuotas maxDepth="1000" maxStringContentLength="1000" maxArrayLength="1000"
            maxBytesPerRead="1000" maxNameTableCharCount="1000" />
        </binding>
      </basicHttpBinding>
      <wsHttpBinding>
        <binding name="wsHttpMtomBinding" closeTimeout="10:01:00" openTimeout="10:01:00"
          receiveTimeout="10:10:00" sendTimeout="10:01:00" maxBufferPoolSize="999999999"
          maxReceivedMessageSize="1048576" messageEncoding="Mtom"
    useDefaultWebProxy="true">
          <readerQuotas maxDepth="100" maxStringContentLength="1000" maxArrayLength="1000"
            maxBytesPerRead="100000" maxNameTableCharCount="1000" />
          <reliableSession inactivityTimeout="10:10:00" />
          <security mode="None" />
        </binding>
      </wsHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <remove scheme="http" />
      <add scheme="http" binding="wsHttpBinding" bindingConfiguration="wsHttpMtomBinding" />
      <add scheme="https" binding="basicHttpsBinding" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
    multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
    <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        Um das Stammverzeichnis der Webanwendung beim Debuggen auszuwählen, legen Sie den Wert unten auf "true" fest.
        Legen Sie ihn vor der Bereitstellung auf "false" fest, um die Veröffentlichung von Informationen über den Webanwendungsordner zu vermeiden.
      -->
    <directoryBrowse enabled="true"/>
    </system.webServer>
    
    </configuration>
    

    客户端:app.config:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IService1" messageEncoding="Mtom">
                    <security mode="None" />
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:2364/Service1.svc" binding="wsHttpBinding"
                bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1"
                name="WSHttpBinding_IService1">
                <identity>
                    <userPrincipalName value="Ares\Admin" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
    </configuration>
    

    客户:Form1.cs

         ...
    
            private void button1_Click(object sender, EventArgs e)
        {
    
            ServiceReference1.IService1 testService = new ServiceReference1.Service1Client();
            ServiceReference1.ServiceRequest testInput = new ServiceReference1.ServiceRequest();
            ServiceReference1.ServiceResponse testOutput = new ServiceReference1.ServiceResponse();
    
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream inoutStream = new MemoryStream();
    
            testInput.Type = "TestType";
            String test = "TestContent";
            testInput.Contents = new MemoryStream();
            formatter.Serialize(testInput.Contents, test);
            //testInput.Contents.Position = 0;
    
            testService.TestCase(testInput);
    
    
            MessageBox.Show("Done!");
    
        }
    
        ...
    

1 个答案:

答案 0 :(得分:0)

我在web.config(端点等)

中看不到任何服务配置数据
    <services>
      <service behaviorConfiguration="..." name="[Namespace].Service1">
        <endpoint address="" binding="basicHttpBinding" bindingNamespace="..." bindingConfiguration="..." contract="[Namespace].IService1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
             <add baseAddress="http://localhost:5000/Service1"/>
...

您可以在运行时配置端点(基本上在自托管服务中),但对于简单方案和IIS基础结构......?

请参阅http://msdn.microsoft.com/en-us/library/ee354381.aspx