rest 服务

2在项目中新建一个名叫test的wcf服务(ps:会同时生成一个itest的接口文件,可以不管他,这里是把这个接口文件删除了)

3、新建Itest Itest2两个接口文件。

4、标注接口为服务契约

5、新建global文件

Itest

1
2
3
4
5
6
7
[ServiceContract(Namespace = " http://localhost:13333/api/", Name = "Test")]
    public interface Itest
    {
        [OperationContract(Name = "test1")]
        [WebGet(BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/{para1}")]
        string test1(string para1);
    }

Itest2

1
2
3
4
5
6
7
[ServiceContract(Namespace = " http://localhost:13333/api/", Name = "testService")]
    public interface Itest2
    {
        [OperationContract(Name = "test2")]
        [WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, UriTemplate = "/{para2}")]
        string test2(string para2);
    }

ServiceContract这个特性声明当前接口为服务契约,OperationContract特性声明当前接口方法为操作契约(如果接口里多个方法,需要指定操作契约(OperationContract)的Name属性)。

这里要注意,如果一个.svc文件要继承实现多个服务契约,必须为每个契约(ServiceContract)指定Namespace Name属性(两个属性的值自己随意指定,但不能重复)。

UriTemplate制定访问这个操作的地质,/{xxx}是要传入的参数,xxx名字必须和操作的参数相同。

test.svc(显式指定Namespace和Name是一个好习惯~)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[ServiceBehavior(Name = "TestServiceHost", Namespace = "http://localhost:13333/api/", ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.Single)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class test : Itest, Itest2
    {
        #region Itest 成员
        [OperationBehavior]
        public string test1(string para1)
        {
            return para1;
        }
                                            
        #endregion
                                            
        #region Itest 成员
                                            
        [OperationBehavior]
        public string test2(string para2)
        {
            return para2;
        }
                                            
        #endregion
    }

web.config

在<configuration></configuration>跟节点中添加

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="wcf.testBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="restful">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="wcf.testBehavior" name="wcf.test">
        <host>
          <baseAddresses>
            <add baseAddress=" http://localhost:8000/wcf/api/"/>
          </baseAddresses>
        </host>
        <endpoint address="test1" binding="webHttpBinding"  behaviorConfiguration="restful" contract="wcf.Itest"></endpoint>
        <endpoint address="test2" binding="webHttpBinding"  behaviorConfiguration="restful" contract="wcf.Itest2"></endpoint>
      </service>
    </services>
  </system.serviceModel>

传送门:wcf配置文件全攻略

Global.asax

1
2
3
4
5
protected void Application_Start(object sender, EventArgs e){
    test service = new test();
    ServiceHost host = new ServiceHost(service);
    host.Open();
}

这段代码是为了寄宿我们的wcf服务

传送门:wcf服务寄宿

ok,搞定,调试下看看效果。

ps:win7下开发的时候,有时会报进程没有权限的错误,把vs关了,用管理员身份打开就ok了。

希望本文对大家有用,搞定难题,心情舒畅,小lol两把,以慰平生~

原文地址:https://www.cnblogs.com/shen119/p/3618839.html