C# 从浏览器运行 WCF 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18989423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run WCF methods from a browser
提问by javaNinja
I am creating a very basic WCF service with C# in Visual Studio 2010. I want to know if I can run my methods directly from a browser by typing something like: //localhost:49815/Service1.svc/methodName(parameterValue)
?
我创建在Visual Studio 2010中使用C#一个非常基本的WCF服务,我想知道如果我可以直接从浏览器输入类似运行我的方法://localhost:49815/Service1.svc/methodName(parameterValue)
?
Here is the essence of my code.
这是我的代码的本质。
Interface:
界面:
using ...
namespace WcfService1{
[ServiceContract]
public interface IService1{
[OperationContract]
[WebGet]
string echoWithGet(string s);
[OperationContract]
[WebInvoke]
string echoWithPost(string s);
}
}
Methods:
方法:
public string echoWithGet(string s ){
return "Get: "+s;
}
public string echoWithPost(string s){
return "Post: " + s;
}
采纳答案by Michael Edenfield
Yes, you can call those methods in a browser, ifyour service is configured properly, though you have the URL syntax wrong.
是的,如果您的服务配置正确,您可以在浏览器中调用这些方法,尽管您的 URL 语法错误。
To call WCF methods from a browser, you need to do two things:
要从浏览器调用 WCF 方法,您需要做两件事:
- Use
[WebGet]
and[WebInvoke]
attributes on your methods, which you have done. Use a
webHttpBinding
for the endpoint of your service and enable thewebHttp
behavior. See http://weblogs.asp.net/kiyoshi/archive/2008/10/08/wcf-using-webhttpbinding-for-rest-services.aspxfor a sample configuration, but the relevant parts are:<service> <endpoint behaviorConfiguration="webBehavior" binding="webHttpBinding" contract="MyServiceContract" /> </service> <endpointBehaviors> <behavior name="webBehavior"> <webHttp /> </behavior> </endpointBehaviors>
- 您已经完成的方法的使用
[WebGet]
和[WebInvoke]
属性。 webHttpBinding
对服务的端点使用 a并启用该webHttp
行为。有关示例配置,请参阅http://weblogs.asp.net/kiyoshi/archive/2008/10/08/wcf-using-webhttpbinding-for-rest-services.aspx,但相关部分是:<service> <endpoint behaviorConfiguration="webBehavior" binding="webHttpBinding" contract="MyServiceContract" /> </service> <endpointBehaviors> <behavior name="webBehavior"> <webHttp /> </behavior> </endpointBehaviors>
Once that is done, WCF will start listening for URL request and route them to your appropriate web methods. You can set up URL templates in your WebGet
or WebPost
attributes that map URL segments to method parameters, if you want to make your URLs "cleaner", but that's optional. Otherwise, you pass parameters the same way you pass parameter to any other URL, using the parameter delimiter:
完成后,WCF 将开始侦听 URL 请求并将它们路由到适当的 Web 方法。如果您想让您的网址“更干净”,您可以在您的WebGet
或WebPost
属性中设置将网址段映射到方法参数的网址模板,但这是可选的。否则,您传递参数的方式与将参数传递给任何其他 URL 的方式相同,使用参数分隔符:
http://localhost:49815/MyService.svc/methodName?parameterName=value
Note that the default for a web-invoked method is a POST. Technically you cando these through a browser but it's much harder (you'd have to make a local HTML form, or use your Javascript console, or something similar), but the WebGet
methods can be invoked just by requesting the correct URL.
请注意,Web 调用方法的默认值是 POST。从技术上讲,您可以通过浏览器执行这些操作,但这要困难得多(您必须制作本地 HTML 表单,或使用您的 Javascript 控制台或类似的东西),但WebGet
只需通过请求正确的 URL 即可调用这些方法。
Also, if your methods return anything more complex than a string, WCF will try to serialize it as JSON; you may need to 'view source' on the resulting page to see it.
此外,如果您的方法返回比字符串更复杂的任何内容,WCF 将尝试将其序列化为 JSON;您可能需要在结果页面上“查看源代码”才能看到它。
回答by petr k.
This does not answer your question, but it will make your life a lot easier to make your service RESTful (or REST-like). While you can do that with WCF, I'd strongly recommend taking a look at ASP.NET Web API.
这并不能回答您的问题,但是让您的服务成为 RESTful(或类似 REST)会让您的生活更轻松。虽然您可以使用 WCF 做到这一点,但我强烈建议您查看ASP.NET Web API。
There are also other alternatives available for creating RESTful services, such as Nancyor ServiceStack.
还有其他替代方案可用于创建 RESTful 服务,例如Nancy或ServiceStack。
回答by maxspan
A straight answer. For GET Methods you can use a browser to view the result. FOR POST Methods you cannot use Browser, if you are directly pasting your url for post method. In order to achieve POST you can either create a HTML FORM or use FIDDLER to see the result.
直截了当的回答。对于 GET 方法,您可以使用浏览器查看结果。FOR POST 方法你不能使用浏览器,如果你直接粘贴你的 url 用于 post 方法。为了实现 POST,您可以创建一个 HTML FORM 或使用 FIDDLER 来查看结果。