1

我有一个需要由第 3 方应用程序调用的 WCF 服务,并发布一些原始 XML。

我正在尝试通过构建一个简单的 WebRequest 并向服务发出请求来测试我的服务。

这是我的服务代码:

界面:

    [ServiceContract(Namespace = "http://test.mydomain.com")]
public interface ITest
{
    [WebInvoke(UriTemplate = "", BodyStyle = WebMessageBodyStyle.Bare, Method="POST")]
    [OperationContract]
    Stream SaveXML(Stream input);
}

服务:

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(Namespace = "http://test.mydomain.com")]
public class Test : ITest
{
    public Stream SaveXML(Stream input)
    {
        StreamReader streamReader = new StreamReader(input);
        string rawString = streamReader.ReadToEnd();
        streamReader.Dispose();

        // here need to save the input stream to xml format file
        Encoding encoding = Encoding.GetEncoding("ISO-8859-1");
        WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
        byte[] returnBytes = encoding.GetBytes(rawString);
        return new MemoryStream(returnBytes);
    }
}

配置:

    <services>
  <service behaviorConfiguration="Blah.TestBehavior" name="Blah.Test">
    <endpoint address="http://localhost:51494/Blah/Test.svc" binding="basicHttpBinding" contract="Blah.ITest">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>

错误的客户端代码:

            string postData = "<Message version=\"1.5\" xmlns=\"http://test.mydomain.com\" ><books>Blah</books></Message>";
        WebRequest request = WebRequest.Create("http://localhost:51494/Blah/Test.svc/SaveXML");
        request.Method = "POST";

        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        request.ContentType = "application/x-www-form-urlencoded";
        //request.ContentType = "text/xml; charset=utf-8";
        //request.ContentType = "text/xml;";
        //request.ContentType = "application/xml;";
        request.ContentLength = byteArray.Length;

        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

        // Get the response.
        WebResponse response = request.GetResponse();

在最后一行我收到 400(错误请求)或 415(不支持的媒体类型)错误,具体取决于我指定的 ContentType。

此外,如果我在客户端应用程序中添加服务引用,并使用 API 调用服务,它工作正常。任何见解都将不胜感激,因为我是 WCF 的新手并且完全被难住了。

4

2 回答 2

7

我认为你在这里混淆了两种不同的东西:

  1. WebRequest并且 aPOST[WebInvoke]属性表明您正在尝试执行类似 REST 的操作
  2. 但是,您的服务配置具有basicHttpBinding- 一个 SOAP 协议,它不会随WebRequest

所以——下定决心!

你想使用 SOAP 吗?然后您可以按原样使用 basicHttpBinding,但是您不能像使用 POST 从 WebRequest 访问 SOAP 服务一样 - 您需要使用 Visual Studio 或svcutil.exe命令行生成的 SOAP 客户端。

你想使用 WebRequest 和一个简单的 POST 请求吗?然后您需要创建一个基于 REST 的 WCF 服务 - 使用webHttpBindingand WebServiceHost(而不是普通的 ServiceHost)。

对于基于 SOAP 的 WCF 服务,请查看MSDN 上的 WCF 开发人员中心

对于基于 REST 的 WCF 服务(您可以在浏览器中导航到,也可以从 WebRequest 调用),请查看MSDN 上的 WCF REST 开发人员中心,并查看Pluralsight关于基于 REST 的开发的优秀截屏系列WCF - 最值得注意的是:

于 2010-03-04T22:14:14.570 回答
1

basicHttpBinding 也适合!

我花了将近一天的时间来弄清楚 WebRequest 到 WCF 服务与 basicHttpBinding 有什么问题,结果我错过了一个小而重要的事情:必须设置SOAPAction标头!

newRequest.Headers["SOAPAction"] = "http://tempuri.org/INotificationService/MyMethodName"
于 2012-07-17T16:12:51.707 回答