• 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两把,以慰平生~

  • 相关阅读:
    Django的自关联
    Django的登录模块
    Django和SQL语句的对应参考
    Django 内置分页的写法
    Django 自定义分页
    经典语句,看看让心灵宁静
    《此生未完成》读后感
    JavaScript杂谈(顺便也当知识积累)
    我总结的js性能优化的小知识
    我总结的js方面你可能不是特别清楚的小知识
  • 原文地址:https://www.cnblogs.com/shen119/p/3618839.html
Copyright © 2020-2023  润新知