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();
}
}
}
}