Restful WCF无效 [英] Restful WCF is not working

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

问题描述

嗨专家,



我现在正在学习WCF基础知识,我尝试了一个示例REST WCF应用程序。当我运行并试图调用该方法时,它显示方法不允许。

当我运行我的服务时,它运行的是网址



http:// localhost:13493 / WCFRestService.svc [ ^ ]



当我通过编辑网址调用方法时

http:// localhost:13493 / WCFRestService.svc / JSONData / mani [ ^ ]

显示为Method not allowed。



我正在使用的代码是

接口

 [ServiceContract] 
public interface IWCFRestService
{
[OperationContract]
[WebInvoke(Method = POST,ResponseFormat = WebMessageFormat.Xml,BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = < span class =code-string> / JSONData / {id})]
string JSONData( string id);
}





继承该界面的类



< pre lang =cs> public class WCFRestService:IWCFRestService
{
< span class =code-keyword> public string JSONData( string id)
{
return 请求的号码 + id;
}
}





和web.config文件已经

 <   system.serviceModel  >  
< 服务 > ;
< service behaviorConfiguration = WCFRestService.WCFRestServiceBehavior name = WCFRestService.WCFRestService >
< 端点 地址 = binding = webHttpBinding behaviorConfiguration = webHttp < span class =code-attribute> contract = WCFRestService.IWCFRestService >
< identity < span class =code-keyword>>
< dns value = 192.168。 100.75 / >
< / identity >
< / endpoint >
< endpoint 名称 = mexHttpBinding 地址 = mex binding = mexHttpBinding 合同 = IMetadataExchange / >
< 主机 >
< baseAddresses >
< span class =code-keyword>< add baseAddress = http://192.168.100.75/WCFService < span class =code-keyword> / >
< / baseAddresses >

< / host >
< / service >
< /服务 >
< 行为 >
< serviceBehaviors >
< behavior 名称 = WCFRestService。 WCFRestServiceBehavior >
< serviceMetadata httpGetEnabled = true / >
< serviceDebug includeExceptionDetailInFaults = false / >
< / behavior >
< / serviceBehaviors >
< < span class =code-leadattribute> endpointBehaviors >
< 行为 名称 = webHttp >
< webHttp / >
< / behavior >
< / endpointBehaviors >
< / behavior >
< / system.serviceModel >

解决方案

我在Ron Beyer的帮助下解决了我的问题



  public   interface  IWCFRestService 
{
[OperationContract]
[WebInvoke(Method = GET,ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = / JSONData / {id})]
string JSONData( string id);
}



必须是GET而不是POST。

SO工作正常,服务已被调用。


Hi experts,

I am learning WCF basics now, and i tried a sample REST WCF application. When i run and tried to invoke the method it shows method not allowed.
When i ran my service, it ran with the url

http://localhost:13493/WCFRestService.svc[^]

and when i invoke the method by editing the url
http://localhost:13493/WCFRestService.svc/JSONData/mani[^]
it displays as Method not allowed.

the code i am using is
Interface

[ServiceContract]
   public interface IWCFRestService
   {
       [OperationContract]
       [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "/JSONData/{id}")]
       string JSONData(string id);
   }



Class that inherited the interface

public class WCFRestService : IWCFRestService
   {
       public string JSONData(string id)
       {
           return "requested number  " + id;
       }
   }



and web.config file has

<system.serviceModel>
        <services>
            <service behaviorConfiguration="WCFRestService.WCFRestServiceBehavior" name="WCFRestService.WCFRestService">
                <endpoint address="" binding="webHttpBinding" behaviorConfiguration="webHttp" contract="WCFRestService.IWCFRestService">
                    <identity>
                        <dns value="192.168.100.75"/>
                    </identity>
                </endpoint>
                <endpoint name="mexHttpBinding" address="mex"  binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://192.168.100.75/WCFService"/>
          </baseAddresses>

        </host>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="WCFRestService.WCFRestServiceBehavior">
                    <serviceMetadata httpGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="false"/>
                </behavior>
            </serviceBehaviors>
            <endpointBehaviors>
                <behavior name="webHttp">
                    <webHttp/>
                </behavior>
            </endpointBehaviors>
        </behaviors>
    </system.serviceModel>

解决方案

I solved my problem with the help from Ron Beyer

public interface IWCFRestService
   {
       [OperationContract]
       [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "/JSONData/{id}")]
       string JSONData(string id);
   }


It must be GET instead of POST.
SO its working fine and service has invoked.


这篇关于Restful WCF无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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