标签:
[ServiceContract]
public interface IUser
{
[OperationContract]
string ShowName(string name);
}
public class User : IUser
{
public string ShowName(string name)
{
string wcfName = string.Format("Show name:{0}", name);
return wcfName;
}
}
Uri baseAddress = new Uri("http://localhost:8080/User");
ServiceHost serviceHost = new ServiceHost(typeof(User),baseAddresses);
host.AddServiceEndpoint(typeof(IUser), new WSHttpBinding(), "User");
//将HttpGetEnabled属性设置为true
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
//将行为添加到Behaviors中
host.Description.Behaviors.Add(smb);
//打开宿主
host.Open();
ServiceHost host = new ServiceHost(typeof(User));
host.Open();
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="WcfServiceLibrary.User">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/User"/>
</baseAddresses>
</host>
<endpoint address="" binding="wsHttpBinding" contract="WcfServiceLibrary.IUser"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
在client中的app.config配置
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IUser" />
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8080/User" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IUser" contract="ServiceReference.IUser"
name="WSHttpBinding_IUser">
<identity>
<userPrincipalName value="xxxx" />
</identity>
</endpoint>
</client>
</system.serviceModel>
标签:
原文地址:http://www.cnblogs.com/xanadu123/p/4607260.html