今天突然想写个超简单的WCF程序,研究了一天才跑通
这玩意配置太复杂了。。。搞半天才配好,而且官网上的帮助例子竟然有错误代码,郁闷死
WinTestWCF_Server服务端项目:
using System.ServiceModel;
namespace WinTestWCF_Server
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService2”。
[ServiceContract]
public interface IService2
{
[OperationContract]
string DoWork(int value);
}
}
namespace WinTestWCF_Server
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“Service2”。
public class Service2 : IService2
{
public string DoWork(int value)
{
testData.count++;
return string.Format("You entered: {0}", value + testData.count + "/n/r"+testData.data);
}
}
public class testData
{
public static string data = @"Data";
public static int count = 0;
}
}
using System;
using System.Windows.Forms;
using System.ServiceModel;
namespace WinTestWCF_Server
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
ServiceHost commonParaHost;
private void btnCreateHost_Click(object sender, EventArgs e)
{
commonParaHost = new ServiceHost(typeof(Service2));
commonParaHost.Faulted += new EventHandler(commonParaHost_Faulted);
commonParaHost.UnknownMessageReceived += new EventHandler<UnknownMessageReceivedEventArgs>(commonParaHost_UnknownMessageReceived);
commonParaHost.Opened += new EventHandler(commonParaHost_Opened);
commonParaHost.Closed += new EventHandler(commonParaHost_Closed);
commonParaHost.Open();
}
void commonParaHost_Closed(object sender, EventArgs e)
{
this.listBox1.Items.Add("Host Is Closed.");
}
void commonParaHost_Opened(object sender, EventArgs e)
{
string temp = "";
foreach (var item in commonParaHost.BaseAddresses)
{
temp += item.Authority;
}
this.listBox1.Items.Add("Host Is Created:" + temp);
}
void commonParaHost_UnknownMessageReceived(object sender, UnknownMessageReceivedEventArgs e)
{
this.listBox1.Items.Add(e.Message.ToString());
}
void commonParaHost_Faulted(object sender, EventArgs e)
{
//throw new NotImplementedException();
}
private void btnCloseHost_Click(object sender, EventArgs e)
{
commonParaHost.Close();
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<!-- 部署服务库项目时,必须将配置文件的内容添加到
主机的 app.config 文件中。System.Configuration 不支持库的配置文件。-->
<system.serviceModel>
<services>
<service name="WinTestWCF_Server.Service2">
<endpoint address="" binding="wsHttpBinding" contract="WinTestWCF_Server.IService2">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Service2/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 为避免泄漏元数据信息,
请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
<serviceMetadata httpGetEnabled="True"/>
<!-- 要接收故障异常详细信息以进行调试,
请将以下值设置为 true。在部署前设置为 false
以避免泄漏异常信息-->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
WinTestWCF_Client客户端项目
using System.ServiceModel;
using WinTestWCF_Server;
namespace WinTestWCF_Client
{
public class CommonParaClient : ClientBase<IService2>, IService2
{
public string DoWork(int value)
{
return base.Channel.DoWork(value);
}
}
}
using System;
using System.Windows.Forms;
namespace WinTestWCF_Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
CommonParaClient client;
private void button1_Click(object sender, EventArgs e)
{
if (client == null)
{
client = new CommonParaClient();
client.InnerChannel.Faulted += new EventHandler(InnerChannel_Faulted);
}
//else
//{
// if (client.State == System.ServiceModel.CommunicationState.Faulted)
// {
// client = new CommonParaClient();
// if (client.State == System.ServiceModel.CommunicationState.Created)
// {
// client.Open();
// }
// }
//}
//MessageBox.Show(client.DoWork(123));
this.textBox1.Text += client.DoWork(123);
//if (client.State != System.ServiceModel.CommunicationState.Closed)
//{
//client.Close();
//}
}
void InnerChannel_Faulted(object sender, EventArgs e)
{
client = new CommonParaClient();
client = new CommonParaClient();
if (client.State == System.ServiceModel.CommunicationState.Created)
{
client.Open();
}
this.textBox1.Text += "/r/n"+"Reopen";
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<!-- 部署服务库项目时,必须将配置文件的内容添加到
主机的 app.config 文件中。System.Configuration 不支持库的配置文件。-->
<system.serviceModel>
<client>
<endpoint
name="WSHttpBinding_IHello"
address="http://localhost:8732/Service2/"
binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IHello"
contract="WinTestWCF_Server.IService2" >
<identity>
<servicePrincipalName value="host/localhost" />
</identity>
</endpoint>
</client>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IHello"
bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard">
<readerQuotas maxDepth="32"/>
<reliableSession ordered="true"
enabled="false" />
<security mode="Message">
</security>
</binding>
<binding name="Another Binding">
</binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>
</configuration>
记得引用System.ServiceModel.dll