wcf调用
(懒惰是遗忘最大的助手)
(本文为方便自己记忆及使用)
1.创建接口
[OperationContract]
[ServiceKnownType(typeof(Model.GenSuperviseLog))]
List<Model.GenSuperviseLog> getSuperviseLog(Guid lid, Guid uid, DateTime begionTime, DateTime endTime);
[ServiceKnownType(typeof(Model.GenSuperviseLog))]
List<Model.GenSuperviseLog> getSuperviseLog(Guid lid, Guid uid, DateTime begionTime, DateTime endTime);
2.实现该接口
代码
public List<Model.GenSuperviseLog> getSuperviseLog(Guid lid, Guid uid, DateTime begionTime, DateTime endTime)
{
List<Model.GenSuperviseLog> ls = new List<Model.GenSuperviseLog>();
for (int i = 0; i < 5; i++)
{
Model.GenSuperviseLog c = new Model.GenSuperviseLog();
c.GenSuperviseLogID = Guid.NewGuid();
c.genSiteBasicID = lid;
c.genEtpRoleBasicID = uid;
c.logBrief = "logBrief ";
c.logCompany = "logCompany ";
c.logContent = "logContent ";
c.logCreator = "logCreator ";
c.logTime = DateTime.Now;
ls.Add(c);
}
return ls;
throw new NotImplementedException();
}
{
List<Model.GenSuperviseLog> ls = new List<Model.GenSuperviseLog>();
for (int i = 0; i < 5; i++)
{
Model.GenSuperviseLog c = new Model.GenSuperviseLog();
c.GenSuperviseLogID = Guid.NewGuid();
c.genSiteBasicID = lid;
c.genEtpRoleBasicID = uid;
c.logBrief = "logBrief ";
c.logCompany = "logCompany ";
c.logContent = "logContent ";
c.logCreator = "logCreator ";
c.logTime = DateTime.Now;
ls.Add(c);
}
return ls;
throw new NotImplementedException();
}
3.服务调用
代码
//获得服务器IP
string hostIP = Dns.GetHostEntry(Dns.GetHostName()).AddressList[2].ToString();
#if CLIENT_DISCOVERY_BUILD
Uri address = new Uri(string.Format("http://localhost:8000/calculator", hostIP));
#else
Uri address = new Uri(string.Format("http://{0}:8000/calculator", hostIP));
#endif
ServiceHost serviceHost = new ServiceHost(typeof(MyProcessService), address);
try
{
// 添加一个服务节点
serviceHost.AddServiceEndpoint(
typeof(IMyProcess),
new BasicHttpBinding(),
"Calculator");
#if !CLIENT_DISCOVERY_BUILD
// 允许运行时的元数据交互(使用NetCfSvcUtil工具)
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
serviceHost.Description.Behaviors.Add(smb);
#endif
serviceHost.Open();
Console.WriteLine("MyProcessService is running at " + address.ToString());
Console.WriteLine("Press <ENTER> to terminate");
Console.ReadLine();
// 把服务状态置为关闭
serviceHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occured: {0}", ce.Message);
serviceHost.Abort();
}
string hostIP = Dns.GetHostEntry(Dns.GetHostName()).AddressList[2].ToString();
#if CLIENT_DISCOVERY_BUILD
Uri address = new Uri(string.Format("http://localhost:8000/calculator", hostIP));
#else
Uri address = new Uri(string.Format("http://{0}:8000/calculator", hostIP));
#endif
ServiceHost serviceHost = new ServiceHost(typeof(MyProcessService), address);
try
{
// 添加一个服务节点
serviceHost.AddServiceEndpoint(
typeof(IMyProcess),
new BasicHttpBinding(),
"Calculator");
#if !CLIENT_DISCOVERY_BUILD
// 允许运行时的元数据交互(使用NetCfSvcUtil工具)
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
serviceHost.Description.Behaviors.Add(smb);
#endif
serviceHost.Open();
Console.WriteLine("MyProcessService is running at " + address.ToString());
Console.WriteLine("Press <ENTER> to terminate");
Console.ReadLine();
// 把服务状态置为关闭
serviceHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occured: {0}", ce.Message);
serviceHost.Abort();
}