运行在浏览器WCF方法 [英] Run WCF methods from a browser

查看:548
本文介绍了运行在浏览器WCF方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建用C#一个很基本的WCF服务在Visual Studio 2010,我想知道如果我可以直接从浏览器通过输入一样的东西跑我的方法: //本地主机:49815 /服务1 .SVC /方法名(的parameterValue)



下面是我的代码的本质。



接口:

 使用... 
命名空间WcfService1 {
[的ServiceContract]
酒店的公共接口IService1 {
[OperationContract的]
[WebGet]
串echoWithGet(字符串s);
[OperationContract的]
[WebInvoke]
串echoWithPost(字符串s);
}
}



方法:

 公共字符串echoWithGet(字符串s){
返回获取+ S;
}

公共字符串echoWithPost(字符串s){
返回后+ S;
}


解决方案

是的,你可以调用这些在浏览器的方法,的如果的服务是否正确配置,虽然你有URL语法错误。



要调用WCF方法从浏览器,你需要做两件事情:




  • 使用 [WebGet] [WebInvoke] 属性上你的方法,你已经做了。

  • 使用的WebHttpBinding 为您服务的终点,并启用 webHttp 行为。请参见 http://weblogs.asp.net /kiyoshi/archive/2008/10/08/wcf-using-webhttpbinding-for-rest-services.aspx 所需的配置,但相关部分是:

     <服务与GT; 
    <端点behaviorConfiguration =webBehavior绑定=的WebHttpBinding合同=MyServiceContract/>
    < /服务>

    < endpointBehaviors>
    <行为NAME =webBehavior>
    < webHttp />
    < /行为>
    < / endpointBehaviors>




一旦做到这一点,WCF将启动监听对于URL请求,并传输给你相应的Web方法。您可以设置URL模板的 WebGet 的WebPost 属性映射URL细分方法的参数,如果你想让你的网址,干净,但这是可选的。否则,你传递参数传递参数给任何其他URL以同样的方式,使用参数分隔符:

 的http://本地主机:49815 / MyService.svc / methodName的参数名称=值

请注意,默认为调用网络方法是一个POST。从技术上讲,你的可以的通过浏览器做这些,但它更难(你必须做一个本地的HTML表单,或使用你的Javascript控制台,或类似的东西),但 WebGet 方法可以只通过请求正确的URL被调用。



另外,如果你的方法返回任何东西不是一个字符串更复杂,WCF会尝试连载它作为JSON;你可能需要查看源代码在结果页中看到它。


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)?

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;
        }

解决方案

Yes, you can call those methods in a browser, if your service is configured properly, though you have the URL syntax wrong.

To call WCF methods from a browser, you need to do two things:

  • Use [WebGet] and [WebInvoke] attributes on your methods, which you have done.
  • Use a webHttpBinding for the endpoint of your service and enable the webHttp behavior. See http://weblogs.asp.net/kiyoshi/archive/2008/10/08/wcf-using-webhttpbinding-for-rest-services.aspx for a sample configuration, but the relevant parts are:

     <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:

http://localhost:49815/MyService.svc/methodName?parameterName=value

Note that the default for a web-invoked method is a POST. Technically you can do 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.

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方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆