REST 和 WCF 连接 [英] REST and WCF connection

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

问题描述

我正在寻找一个使用 a) WCF & 的示例.休息.经过长时间的谷歌搜索,虽然我得到了一些,但它们超出了我的理解.

I am specifically looking for an example which use a) WCF & REST. After a long googling, though I got some but they are beyond my understanding.

有人可以给我一个非常简单的例子,比如神圣世界"或 2 个数字的总和,这将使我清楚地了解如何编写服务器,以及如何从客户端使用它.

Could some one please give me a very simple example say "Hallow World" or Summation of 2 numbers which will give me a clear insight about how to write a server, and also how to consume the same from the client end.

另外,如果有任何用简单术语解释此类示例的好的链接,请告诉我.

Also if any good link that explains this kind of example in simple terms kindly tell me that.

谢谢

推荐答案

WCF 中的 REST 一旦你弄明白了就不是那么难了.

REST in WCF is not that hard once you figure it out.

首先你必须定义你的接口.

First you must define your interface.

这是一个例子.

[ServiceContract]
public interface IRESTExample
{
    [WebGet(UriTemplate = "interaction/queue?s={site}", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
    [OperationContract]
    string QueueInteraction(string site);

    [WebGet(UriTemplate = "interaction/cancel?id={interactionId}", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
    [OperationContract]
    string CancelInteraction(string interactionId);

    [WebGet(UriTemplate = "queue/state?s={site}&q={queue}", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
    [OperationContract]
    string QueueState(string site, string queue);

}

您可以在 WebGet 中看到您定义的最终 URL.所以这取决于你在哪里绑定它,但假设你将端点绑定到 www.example.com/rest

You can see in the WebGet you define the final URL. So it depends on where you bind it, but say you bind the endpoint to www.example.com/rest

QueueInteraciton 将是 www.example.com/rest/interaction/queue?s=SomeSite

QueueInteraciton would be www.example.com/rest/interaction/queue?s=SomeSite

其中 {stie} 或 {parameterName} 替换为参数名称.

Where {stie} or {parameterName} is replaced with the name of the parameter.

实现只是一个简单的类,我假设您知道如何实现接口.如果您需要帮助,请发表评论.

The implemetion is just a simple class, I am going to assume you know how to implement an interface. If you need help just leave a comment.

现在绑定端点.最后也没有那么难,你可以在配置中完成.

Now binding the endpoint. In the end it is not that hard, you can do it all in the config.

<system.serviceModel>
    <services>
        <service name="Stackoverflow.Example.Service.RestExample" behaviorConfiguration="MyServiceTypeBehaviors">
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:2136/RestExample"/>
                </baseAddresses>
            </host>

            <endpoint address="rest" binding="webHttpBinding" behaviorConfiguration="xmlBehavior" contract="Stackoverflow.Example.Service.IRESTExample" />

        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="MyServiceTypeBehaviors" >
                <!-- Add the following element to your service behavior configuration. -->
                <serviceMetadata httpGetEnabled="true" />
            </behavior>
        </serviceBehaviors>

        <endpointBehaviors>
            <behavior name="jsonBehavior">
                <webHttp/>
            </behavior>
            <behavior name="xmlBehavior">
                <webHttp/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <bindings>
        <basicHttpBinding>
            <binding name = "NoSecurity">
                <security mode = "None" />
            </binding>
        </basicHttpBinding>
    </bindings>
</system.serviceModel>

现在是启动服务并绑定它的代码.你可以在任何东西中做到这一点,例如控制台应用.

Now the code to start the service and bind it. YOu can do it in anything, for example a console app.

RestExample exampleService = new RestExample();

host = new ServiceHost(exampleService);

host.Open();

这应该足以开始.

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

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