如何在basicHttpBinding中将SOAP WCF与网站结合起来

时间:2015-10-15 12:10:36

标签: c# asp.net web-services wcf soap

好的,情况是这样的,我想在现有的asp.net网站上添加一个存在的WCF服务,但是我遇到了头疼400问题:

  • 我有一个SOAP WCF服务发送大文件流,效果很好。

FileUploadService.cs是这样的,接口配置得很好。

public class FileUploadService : IFileUploadService
{
    private readonly string _path = Directory.GetCurrentDirectory() + \\sharedfiles\\";
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }
    public void UploadPocoStream(PocoStream pocoStream)
    {
        try
        {
            if (!Directory.Exists(_path))
            {
                Directory.CreateDirectory(_path);
            }
            using (FileStream outputStream = new FileStream(_path + pocoStream.FileName, FileMode.OpenOrCreate, FileAccess.Write))
            {
                pocoStream.Stream.CopyTo(outputStream);
                outputStream.Flush();
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
        }
    }
}

和PocoStream是这样的:

[MessageContract]
public class PocoStream
{
    [MessageHeader]
    public string FileName { get; set; }

    [MessageBodyMember]
    public Stream Stream { get; set; }
}
  • 我要将WCF服务与现有的演示网站结合起来。

以下是服务器属性:

enter image description here

我将此添加到网站的web.config文件中:

<system.serviceModel>
  <services>
    <service name="test1.FileUploadService">
      <host>
        <baseAddresses>
          <add baseAddress="http://localhost:2668/"/>
        </baseAddresses>
      </host>
      <!-- Service Endpoints -->
      <endpoint address="FileUploadService.svc" binding="basicHttpBinding"
                contract="test1.IFileUploadService" bindingConfiguration="haha" >
        <identity>
          <dns value="localhost"/>
        </identity>
      </endpoint>
      <!-- Metadata Endpoints -->
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="">
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <bindings>
    <basicHttpBinding>
      <binding name="haha" closeTimeout="0:10:00" 
               receiveTimeout="00:20:00" sendTimeout="00:20:00" maxBufferSize="65536" 
               maxReceivedMessageSize="2147483647000" transferMode="StreamedRequest"/>
    </basicHttpBinding>
  </bindings>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
  • 我成功添加了服务引用并构建了一个测试客户端。

测试客户端是这样的:

FileUploadServiceClient a = new FileUploadServiceClient();
using (FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read))
{
    var b = new PocoStream() { FileName = fileName, Stream = fs };
    a.UploadPocoStream(b.FileName, b.Stream);
}

和客户端的app.config:

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding name="BasicHttpBinding_IFileUploadService" closeTimeout="0:10:00" receiveTimeout="00:20:00" sendTimeout="00:20:00" maxBufferSize="65536" maxReceivedMessageSize="2147483647000" transferMode="StreamedRequest"/>
    </basicHttpBinding>
  </bindings>
    <client>
        <endpoint address="http://localhost:2668/FileUploadService.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFileUploadService"
            contract="ServiceReference1.IFileUploadService" name="BasicHttpBinding_IFileUploadService" />
    </client>
</system.serviceModel>

但结果是,一旦运行客户端,我在客户端遇到了意外的400错误请求: enter image description here

这在服务器上: enter image description here

我认为配置文件出错了。任何人都可以帮忙吗?非常感谢!

说明:

  1. 我可以成功创建文件,但它是空的。

  2. 我正在尝试使用“Stream”transferMode上传内存成本很低的大文件。

  3. 我没有设置任何安全设置,网站只是一个演示网站。

1 个答案:

答案 0 :(得分:0)

您应该使用customBinding而不是basicHttpBinding。它更加可定制。

您可以使用此工具制作它:http://webservices20.cloudapp.net/

然后,您只需配置HTTP(S)传输即可添加基本身份验证:

<customBinding>
  <binding name="NewBinding0">
    <textMessageEncoding MessageVersion="Soap11" />
    <httpTransport maxReceivedMessageSize="2147483647000" transferMode="StreamedRequest" authenticationScheme="Basic" />
  </binding>
</customBinding>

仅当您在SSL / TLS下公开服务时才使用httpsTranport。虽然,请保留httpTransport元素。

在客户端中,添加用户名和密码:

a.ClientCredentials.UserName.UserName = "UserName";
a.ClientCredentials.UserName.Password = "Password";

然后确认IIS Web应用程序中已启用基本身份验证。