C# 远程服务器返回错误:(405)方法不允许。WCF休息服务

C# 远程服务器返回错误:(405)方法不允许。WCF休息服务,c#,.net,wcf,rest,C#,.net,Wcf,Rest,其他地方已经提出了这个问题,但这些问题并不是解决我问题的办法 这是我的服务 [WebInvoke(UriTemplate = "", Method = "POST")] public SampleItem Create(SampleItem instance) { // TODO: Add the new instance of SampleItem to the collection // throw new NotImplementedException(); ret

其他地方已经提出了这个问题,但这些问题并不是解决我问题的办法

这是我的服务

[WebInvoke(UriTemplate = "", Method = "POST")]
public SampleItem Create(SampleItem instance)
{
    // TODO: Add the new instance of SampleItem to the collection
    // throw new NotImplementedException();
    return new SampleItem();
}
我有这个代码来调用上述服务

XElement data = new XElement("SampleItem",
                             new XElement("Id", "2"),
                             new XElement("StringValue", "sdddsdssd")
                           ); 

System.IO.MemoryStream dataSream1 = new MemoryStream();
data.Save(dataSream1);

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:2517/Service1/Create");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
// You need to know length and it has to be set before you access request stream
request.ContentLength = dataSream1.Length;

using (Stream requestStream = request.GetRequestStream())
{
    dataSream1.CopyTo(requestStream);
    byte[] bytes = dataSream1.ToArray();
    requestStream.Write(bytes, 0, Convert.ToInt16(dataSream1.Length));
    requestStream.Close();
}

WebResponse response = request.GetResponse();
我在最后一行得到一个异常:


远程服务器返回错误:(405)方法不允许。不确定为什么会发生这种情况,我尝试将主机从VS服务器更改为IIS,但结果没有改变。如果您需要更多信息,请告诉我您是第一次运行WCF应用程序吗

运行下面的命令以注册wcf

"%WINDIR%\Microsoft.Net\Framework\v3.0\Windows Communication Foundation\ServiceModelReg.exe" -r

首先要知道REST服务的确切URL。因为您指定了
http://localhost:2517/Service1/Create
现在,只要尝试从IE打开相同的URL,您就应该得到不允许的方法,因为您的
Create
方法是为WebInvoke定义的,IE执行WebGet

现在,请确保在服务器上的同一名称空间中定义了客户端应用程序中的SampleItem,或者确保正在生成的xml字符串具有适当的名称空间,以便服务能够标识示例对象的xml字符串可以反序列化回服务器上的对象

我在服务器上定义了SampleItem,如下所示:

namespace SampleApp
{
    public class SampleItem
    {
        public int Id { get; set; }
        public string StringValue { get; set; }            
    }    
}
string sample = "<SampleItem xmlns=\"http://schemas.datacontract.org/2004/07/XmlRestService\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><Id>6</Id><StringValue>from client testing</StringValue></SampleItem>";   
string serviceBaseUrl = "http://localhost:2517/Service1";
string resourceUrl = "/Create";
string method="POST";             
UseHttpWebApproach<string>(serviceBaseUrl, resourceUrl, method, sample);
我的SampleItem对应的xml字符串如下所示:

<SampleItem xmlns="http://schemas.datacontract.org/2004/07/SampleApp" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Id>6</Id><StringValue>from client testing</StringValue></SampleItem>
对上述方法的调用如下所示:

namespace SampleApp
{
    public class SampleItem
    {
        public int Id { get; set; }
        public string StringValue { get; set; }            
    }    
}
string sample = "<SampleItem xmlns=\"http://schemas.datacontract.org/2004/07/XmlRestService\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><Id>6</Id><StringValue>from client testing</StringValue></SampleItem>";   
string serviceBaseUrl = "http://localhost:2517/Service1";
string resourceUrl = "/Create";
string method="POST";             
UseHttpWebApproach<string>(serviceBaseUrl, resourceUrl, method, sample);
string sample=“6来自客户端测试”;
字符串serviceBaseUrl=”http://localhost:2517/Service1";
字符串resourceUrl=“/Create”;
string method=“POST”;
UseHttpWebApproach(serviceBaseUrl、resourceUrl、方法、示例);

注意:只需确保您的URL是正确的

在花了2天的时间,使用VS 2010.NET 4.0、IIS 7.5 WCF和REST以及JSON ResponseWrapped之后,我终于通过阅读这里的“进一步调查时…”破解了它

Web服务客户端代码生成的文件Reference.cs不将
GET
方法与
[WebGet()]
属性化,因此尝试
发布
方法,因此不允许使用InvalidProtocol,405方法。问题是,刷新服务引用时会重新生成此文件,对于WebGet属性,还需要对
System.ServiceModel.Web
的dll引用

所以我决定手动编辑Reference.cs文件,并保留一份副本。下次我刷新它时,我将把我的
WebGet()合并回来


在我看来,svcutil.exe存在一个缺陷,它没有认识到某些服务方法是
GET
,而不仅仅是
POST
,即使WCF IIS web服务发布的WSDL和帮助确实了解哪些方法是
POST
GET
???我已经用Microsoft Connect记录了这个问题。

当它发生在我身上时,我只是简单地添加了单词
post

函数名,它解决了我的问题。也许它也会对你们中的一些人有所帮助。

在我遇到的情况中,还有另一个原因:底层代码试图执行PUT。(此特定应用程序可配置为在需要时启用此功能;我不知道该功能已启用,但未设置必要的web服务器环境


希望这可以帮助其他人。

我已经解决了这个问题,因为您的服务是由用户名和密码的登录凭据保护的,请尝试在请求中设置用户名和密码,它会工作的。祝您好运!

您的路由看起来像什么?请添加您正在使用的任何绑定配置/代码。您正在设置contenttype设置为“application/x-www-form-urlencoded”。但您正在发送xml数据。能否将内容类型设置为“application/xml”我没有任何绑定配置我正在获取远程服务器返回的错误:(405)方法不允许。您的服务是如何托管的?您在global.asax中是否有条目。还要检查您是否启用了帮助页面,并从中尝试查找您试图发布的方法的url,并查看url是否正确帮助正在工作我可以在浏览器中查看服务,get正在浏览器中工作。服务通过VS Server和我在global.asaxc中注册了路由。您可以删除空的URITemplate属性并查看其是否有效。此外,您的帮助页面将在后期提供创建方法的URL,而在帮助期间提供创建URL。我删除了URi模板,但仍然收到405异常
string sample = "<SampleItem xmlns=\"http://schemas.datacontract.org/2004/07/XmlRestService\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><Id>6</Id><StringValue>from client testing</StringValue></SampleItem>";   
string serviceBaseUrl = "http://localhost:2517/Service1";
string resourceUrl = "/Create";
string method="POST";             
UseHttpWebApproach<string>(serviceBaseUrl, resourceUrl, method, sample);