WCF实现客户端和服务端

本文详细介绍了如何在服务端和服务接口中定义ServiceContract,并通过ServiceHost和Addendpoint方法实现服务发布;同时,展示了如何在客户端获取代理并进行服务调用的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

service side

1.定义ServiceContract:

2.new a ServiceHost

3. add endpoint

using System.ServiceModel;

namespace Service
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri address = new Uri("http://localhost:8000/myservice");
            using (ServiceHost host = new ServiceHost(typeof(MyService), address))      //MyService是服务方法的类库名称
            {
                host.AddServiceEndpoint(typeof(IMyService), new BasicHttpBinding(), address);   //IMyService是服务接口类库名称
                host.Open();
                Console.ReadLine();
            }
        }

    }

    [ServiceContract]
    public interface IMyService
    {
        [OperationContract]
        string GetService();
    }

    public class MyService : IMyService
    {

        public string GetService()
        {
            return "Got Service!";
        }
    }
}

client side: get proxy, service 调用

using System.ServiceModel;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            IMyService proxy = ChannelFactory<IMyService>.CreateChannel(new BasicHttpBinding(),
                new EndpointAddress("http://localhost:8000/myservice"));
            Console.WriteLine(proxy.GetService());
            Console.ReadLine();
        }
    }

    [ServiceContract]
    public interface IMyService
    {
        [OperationContract]
        string GetService();
    }

}

 

 

 bdc = new BaseDataClient(new BasicHttpBinding(), new EndpointAddress(Configurator.GetConfigValue("stsurl")));
    <add key="stsurl" value="http://192.168.100.1:8888/CoreData.svc" />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值