first wcf

本文详细介绍了如何使用Windows Communication Foundation (WCF)搭建一个简单服务,并在客户端进行调用的过程。包括服务端的创建、配置及启动,以及客户端的调用流程,展示了WCF在实现跨平台、跨语言服务通信的能力。

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Client.ServiceReference1;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceReference1.HelloWCFServiceClient proxy = new HelloWCFServiceClient();
            string str = proxy.HelloWCF("欢迎来到WCF村!");
            Console.WriteLine(str);
            Console.ReadLine();
           
        }
    }
}
App.Config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="Host.HelloWCFServiceBehavior">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service behaviorConfiguration="Host.HelloWCFServiceBehavior"
                name="Host.HelloWCFService">
                <endpoint address="" binding="wsHttpBinding" contract="Host.IHelloWCFService">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8731/HelloWCFService" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
</configuration>
HelloWCFService:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace Host
{
    // 注意: 如果更改此处的类名 "HelloWCFService",也必须更新 App.config 中对 "HelloWCFService" 的引用。
    public class HelloWCFService : IHelloWCFService
    {
        public  string HelloWCF(string message)
        {
            return string.Format("你在{0}收到信息:{1}",DateTime.Now,message);
        }
    }
}

 

IHelloWCFService:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace Host
{
    // 注意: 如果更改此处的接口名称 "IHelloWCFService",也必须更新 App.config 中对 "IHelloWCFService" 的引用。
    [ServiceContract]
    public interface IHelloWCFService
    {
        [OperationContract]
        string HelloWCF(string message);
    }
}

Program:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Host
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(Host.HelloWCFService)))
            {
                host.Open();
                Console.ReadLine();
                host.Close();
            }

        }
    }
}

 

 

 

转载于:https://www.cnblogs.com/sweting/archive/2010/02/24/1672690.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值