Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Wcf Restful服务提供了不允许的方法或错误的请求_C#_Wcf_Wcf Rest - Fatal编程技术网

C# Wcf Restful服务提供了不允许的方法或错误的请求

C# Wcf Restful服务提供了不允许的方法或错误的请求,c#,wcf,wcf-rest,C#,Wcf,Wcf Rest,我的服务已定义为: [ServiceContract(ProtectionLevel = ProtectionLevel.None)] [XmlSerializerFormat] public interface IMSMService { [OperationContract(IsOneWay = false)] [WebInvoke(Method = "POST", UriTemplate = "GetProducts", BodyStyle = WebMessageBod

我的服务已定义为:

[ServiceContract(ProtectionLevel = ProtectionLevel.None)]
[XmlSerializerFormat]
public interface IMSMService
{

    [OperationContract(IsOneWay = false)]
    [WebInvoke(Method = "POST", UriTemplate = "GetProducts", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
    ProductsResponse GetProducts(ProductsRequest request);

}
方法头的形式如下:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MSMService : IMSMService
{.....
我运行服务,然后在客户端应用程序中对wcf进程进行服务引用。在我的客户机测试项目中,我有以下测试

 [Test]
    public void CallWCFRestful()
    {
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(ProductsRequest));

        MemoryStream stream = new MemoryStream();

        xmlSerializer.Serialize(stream,_productsRequest);

        string data = Encoding.UTF8.GetString(stream.ToArray(), 0, (int)stream.Length);
       string link = "http://localhost/WcfService/GetProducts";
       var request = (HttpWebRequest)WebRequest.Create(new Uri(link));

        request.ContentType = "application/soap+xml; charset=utf-8";
        request.Method = "POST";
        request.ContentLength = data.Length;
        request.KeepAlive = true;

        using (var requestStream = request.GetRequestStream())
        {

            var writer = new StreamWriter(requestStream);

            writer.Write(data);

            writer.Flush();

        }

        using (HttpWebResponse resp = request.GetResponse() as HttpWebResponse)
        {

            using (var responseStream = resp.GetResponseStream())
            {

                var reader = new StreamReader(responseStream);

                var result = reader.ReadToEnd();

            }

        }
    }

在代码中,当我运行服务时,它会在尝试使用错误请求错误设置“resp”时出错。如果按住ctrl键并单击,则显示服务正在运行。如果我按照我的代码行包含该方法,那么我将收到methodnotallowed(我希望是这样,因为我没有传递对象)。我做错了什么。

因为这是一个WCF REST服务,所以内容类型不能是SOAP。它需要是application/xml。尝试更改内容类型并再次测试。这可能是原因=,或者至少是其中一个原因

更改如下:

request.ContentType = "application/xml; charset=utf-8";

链接是否有效。我是说服务端点?