C# 不允许使用WCF服务/方法
我正在尝试运行WCF服务。因此,我扩展了MSDN中的一个示例,但是如果我调用“EchoPerson()”或“EchoWithTest(s string)”,我将得到一个错误“405 Method not allowed”。如果我从代码(channel.EchoPerson())调用它,它将工作;如果我从Webbrowser使用GET调用它,它将不工作C# 不允许使用WCF服务/方法,c#,wcf,C#,Wcf,我正在尝试运行WCF服务。因此,我扩展了MSDN中的一个示例,但是如果我调用“EchoPerson()”或“EchoWithTest(s string)”,我将得到一个错误“405 Method not allowed”。如果我从代码(channel.EchoPerson())调用它,它将工作;如果我从Webbrowser使用GET调用它,它将不工作 using System; using System.Collections.Generic; using System.ServiceModel
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using System.Text;
using System.Web.Script.Serialization;
namespace ConsoleApplication2
{
[ServiceContract]
public interface IService
{
[OperationContract]
[WebGet]
string EchoWithGet(string s);
[OperationContract]
[WebInvoke]
string EchoWithPost(string s);
[OperationContract]
[WebInvoke]
string EchoWithTest(string s);
[OperationContract]
[WebInvoke]
string EchoPerson();
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public double Shoesize { get; set; }
}
public class Service : IService
{
public string EchoWithGet(string s)
{
return "You said " + s;
}
public string EchoWithTest(string s)
{
return "You said " + s;
}
public string EchoWithPost(string s)
{
return "You said " + s;
}
//[WebInvoke(Method = "GET",
// ResponseFormat = WebMessageFormat.Json,
// UriTemplate = "data/{id}")]
public string EchoPerson()
{
Person testPerson = new Person()
{
Name = "TestPerson",
Age = 12,
Shoesize = 9.5
};
string s1 = new JavaScriptSerializer().Serialize(testPerson);
return s1;
}
}
class Program
{
static void Main(string[] args)
{
WebServiceHost host = new WebServiceHost(typeof(Service), new Uri("http://localhost:8000/"));
try
{
ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "");
host.Open();
using (ChannelFactory<IService> cf = new ChannelFactory<IService>(new WebHttpBinding(), "http://localhost:8000"))
{
cf.Endpoint.Behaviors.Add(new WebHttpBehavior());
IService channel = cf.CreateChannel();
string s;
Console.WriteLine("Calling EchoWithGet via HTTP GET: ");
s = channel.EchoWithGet("Hello, world");
Console.WriteLine(" Output: {0}", s);
Console.WriteLine("");
Console.WriteLine("This can also be accomplished by navigating to");
Console.WriteLine("http://localhost:8000/EchoWithGet?s=Hello, world!");
Console.WriteLine("in a web browser while this sample is running.");
Console.WriteLine("");
s = channel.EchoPerson();
Console.WriteLine(" Output: {0}", s);
Console.WriteLine("Calling EchoWithPost via HTTP POST: ");
s = channel.EchoWithPost("Hello, world");
Console.WriteLine(" Output: {0}", s);
Console.WriteLine("");
}
Console.WriteLine("Press <ENTER> to terminate");
Console.ReadLine();
host.Close();
}
catch (CommunicationException cex)
{
Console.WriteLine("An exception occurred: {0}", cex.Message);
Console.ReadLine();
host.Abort();
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.ServiceModel;
使用System.ServiceModel.Description;
使用System.ServiceModel.Web;
使用系统文本;
使用System.Web.Script.Serialization;
命名空间控制台应用程序2
{
[服务合同]
公共接口设备
{
[经营合同]
[WebGet]
字符串EchoWithGet(字符串s);
[经营合同]
[WebInvoke]
字符串EchoWithPost(字符串s);
[经营合同]
[WebInvoke]
字符串EchoWithTest(字符串s);
[经营合同]
[WebInvoke]
字符串EchoPerson();
}
公共阶层人士
{
公共字符串名称{get;set;}
公共整数{get;set;}
公共双鞋码{get;set;}
}
公共课服务:IService
{
公共字符串EchoWithGet(字符串s)
{
返回“yousaid”+s;
}
公共字符串EchoWithTest(字符串s)
{
返回“yousaid”+s;
}
公共字符串EchoWithPost(字符串s)
{
返回“yousaid”+s;
}
//[WebInvoke(Method=“GET”,
//ResponseFormat=WebMessageFormat.Json,
//UriTemplate=“data/{id}”)]
公共字符串EchoPerson()
{
Person testPerson=新的Person()
{
Name=“TestPerson”,
年龄=12岁,
Shoesize=9.5
};
字符串s1=新的JavaScriptSerializer().Serialize(testPerson);
返回s1;
}
}
班级计划
{
静态void Main(字符串[]参数)
{
WebServiceHost主机=新的WebServiceHost(服务类型),新的Uri(“http://localhost:8000/"));
尝试
{
ServiceEndpoint ep=host.AddServiceEndpoint(typeof(IService),新的WebHttpBinding(),“”);
host.Open();
使用(ChannelFactory cf=new ChannelFactory(new WebHttpBinding(),”http://localhost:8000"))
{
添加(新的WebHttpBehavior());
IService channel=cf.CreateChannel();
字符串s;
WriteLine(“通过HTTP GET调用EchoWithGet:”;
s=channel.EchoWithGet(“你好,世界”);
WriteLine(“输出:{0}”,s);
控制台。写线(“”);
Console.WriteLine(“这也可以通过导航到来实现”);
控制台写入线(“http://localhost:8000/EchoWithGet?s=Hello“世界!”;
WriteLine(“在运行此示例时在web浏览器中”);
控制台。写线(“”);
s=channel.EchoPerson();
WriteLine(“输出:{0}”,s);
WriteLine(“通过HTTP POST调用EchoWithPost:”);
s=channel.EchoWithPost(“你好,世界”);
WriteLine(“输出:{0}”,s);
控制台。写线(“”);
}
控制台。写入线(“按下以终止”);
Console.ReadLine();
host.Close();
}
捕获(通信异常cex)
{
WriteLine(“发生异常:{0}”,cex.Message);
Console.ReadLine();
host.Abort();
}
}
}
}
提前谢谢 这听起来像是预期的行为
WebInvoke
=POST
,WebGet
=GET
。您可以使用Fiddler或CURL之类的工具来构建一个POST
测试请求。奇怪的是,它可以处理带有405错误的GET-but-answers。我看不出两种方法之间的区别。合同决定了可接受的方法<代码>[WebInvoke]字符串EchoWithTest(字符串s)代码>正在强制执行POST
。如果希望它是GET
,请将其更改为[WebGet]字符串EchoWithTest(字符串s)代码>谢谢,这就是问题所在!愚蠢的是我没有亲眼看到!!