3

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

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

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

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

  1. 来自 Visual Studio 2012 和 IIS Express 8.0 的集成 Microsoft 开发服务器上的相同错误

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

  3. 试图在两台服务器上生成日志文件。IIS Express 服务器在配置文件之外的其他文件夹中生成一些日志,但不使用我的配置文件中的设置。Microsoft 开发服务器不生成任何内容。

  4. 我使用 svctraceviewer.exe 来查看生成的几个日志文件,并且刚刚看到一些带有操作码 MODULE_SET_RESPONSE_ERROR_STATUS 的警告。我无法理解这些信息。

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

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

如果您需要更多信息,请告诉我。

谢谢!

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

服务: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!");

    }

    ...
4

1 回答 1

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

于 2012-11-27T10:01:28.647 回答