1.1 创建一个WCF应用程序服务

第一步:引入System.ServiceModel.dll

第二步 定义一个WCF接口:

复制代码
  //定义接口
    [ServiceContract]
    interface IGetInfo
    {
        [OperationContract]
        DateTime GetDateTime();
        [OperationContract]
        string GetName();
        #region 重载
        [OperationContract(Name = "say_name")]
        string Say(string name);
        [OperationContract(Name = "say_name_adress")]
        string Say(string name, string address); 
        #endregion
    }
复制代码

第三步 实现WCF接口:

复制代码
      //实现IGetInfo接口
    class GetInfoService : IGetInfo
    {
        public DateTime GetDateTime()
        {
            return DateTime.Now;
        }

        public string GetName()
        {
            return "张三";
        }

        public string Say(string name)
        {
            return name+"说话了";
        }

        public string Say(string name,string address)
        {
            return name+""+address+"说话了";
        }
    }
复制代码

 

第四步 修改App.config配置:

 

 

复制代码
<!--添加的内容-->
  <system.serviceModel>
    <services>
      <service  name="WCFDEMO2.GetInfoService" behaviorConfiguration="TestBehaciors">
        <host>
          <!--基地址 baseAddress启动服务访问地址-->
          <baseAddresses>
            <add baseAddress="http://localhost:9001/GetInfo"/>
          </baseAddresses>
        </host>
        <!--终节点 binding 访问的协议 -->
        <endpoint address="" binding="basicHttpBinding" contract="WCFDEMO2.IGetInfo" ></endpoint>
      </service>
    </services>
    <!--行为可配置可不配置-->
    <behaviors>
      <serviceBehaviors>
        <behavior name="TestBehaciors">
          <!--允许访问WCF的服务-->
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
复制代码

第五步 启动服务:

 ServiceHost host = new ServiceHost(typeof( WCFDEMO2.GetInfoService));
            host.Open();

 

posted @   RC城  阅读(79)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
点击右上角即可分享
微信分享提示