1.先创建一个WCF服务库
2.创建一个Console控制台,服务将寄宿在该应用程序上,该程序一旦关闭,服务将停止。
控制台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WcfServiceLibrary;
using System.ServiceModel;
using System.ServiceModel.Description; namespace wcfConsole
{
class Program
{
static void Main(string[] args)
{
//这是将来要引用的服务地址,由你自己定义
Uri address = new Uri("http://localhost:8123/ServiceDemo/Service"); //MyService是你WCF服务库里面服务的名称
ServiceHost selfHost = new ServiceHost(typeof(MyService), address); try
{
//IService是你服务库里面接口的名称
selfHost.AddServiceEndpoint(typeof(IService), new WSHttpBinding(), "MyService"); //设置服务行为
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb); //打开 ServiceHost
selfHost.Open();
Console.WriteLine("success");
Console.WriteLine();
Console.ReadLine(); // 关闭 ServiceHost
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("Error : {0}", ce.Message);
selfHost.Abort();
}
}
}
}
也可以用 app.config 配置服务信息,代码要略作修改。
app.config内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="WcfServiceLibrary.MyService" behaviorConfiguration="MyBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8123/ServiceDemo/Service" />
</baseAddresses>
</host>
<endpoint address="MyService" binding="basicHttpBinding"
contract="WcfServiceLibrary.IService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
service.name 是你WCF服务库里服务类的完全限定名
service.behaviorConfiguration 要与 behavior.name 相同
endpoint.contract 是你WCF服务库里接口的完全限定名
改动后的控制台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WcfServiceLibrary;
using System.ServiceModel;
using System.ServiceModel.Description; namespace wcfConsole
{
class Program
{
static void Main(string[] args)
{
//这是将来要引用的服务地址,由你自己定义
//Uri address = new Uri("http://localhost:8123/ServiceDemo/Service"); //MyService是你WCF服务库里面服务的名称
ServiceHost selfHost = new ServiceHost(typeof(MyService)); try
{
////IService是你服务库里面接口的名称
//selfHost.AddServiceEndpoint(typeof(IService), new WSHttpBinding(), "MyService"); ////设置服务行为
//ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
//smb.HttpGetEnabled = true;
//selfHost.Description.Behaviors.Add(smb); //打开 ServiceHost
selfHost.Open();
Console.WriteLine("success");
Console.WriteLine();
Console.ReadLine(); // 关闭 ServiceHost
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("Error : {0}", ce.Message);
selfHost.Abort();
}
}
}
}
测试:
先运行控制台,然后新建项目添加服务引用:http://localhost:8123/ServiceDemo/Service
直接调用WCF服务库默认提供的方法:GetData()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace InvokeWcfServiceTest
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ServiceReference.ServiceClient client = new ServiceReference.ServiceClient();
Response.Write(client.GetData());
Response.End();
}
}
}
结果是可喜的。