尝试使用WebRequest调用WCF服务

1 c# rest wcf post webrequest

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

我试图通过构建一个简单的WebRequest并向服务发出请求来测试我的服务.

这是我的服务代码:

接口:

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

服务:

    [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);
    }
}
Run Code Online (Sandbox Code Playgroud)

配置:

    <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>
Run Code Online (Sandbox Code Playgroud)

有问题的客户代码:

            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();
Run Code Online (Sandbox Code Playgroud)

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

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

mar*_*c_s 7

我想你在这里混合了两件不同的东西:

  1. WebRequest和a POST[WebInvoke]属性表明你正在尝试做一些类似REST的事情
  2. 但是,您的服务配置具有basicHttpBinding- 不会随附的SOAP协议WebRequest

所以 - 下定决心!

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

你想使用WebRequest和简单的POST请求吗?然后,您需要创建基于REST的WCF服务 - 使用webHttpBindingWebServiceHost(而不是简单的ServiceHost).

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

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