延續 WCF教學(4) - 設計Console Client
本練習利用程式的方式,建立ServiceHost物件裝載服務,並透過程式加入服務Endpoint
- 刪掉 Host app.config組態檔 (ConsoleApplication1)
- 修改Host(ConsoleApplication1) ,Main方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using WcfServiceLibrary1;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Service is running...");
ServiceHost myHost = new
ServiceHost(typeof(Calc));
myHost.AddServiceEndpoint(typeof(IMyService),
new BasicHttpBinding(),
"http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/");
myHost.Open();
Console.ReadLine();
}
}
}
- 按CTRL + F5執行,Client可以正確呼叫Service
目前程式雖然可以正確執行,但因為沒有開放Metadata端點,因此,當你執行Host (ConsoleApplication1) 後,開啟Browser,輸入以下URL
http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/mex
你將無法取得WSDL
- 修改Main方法,建立ServiceMetadataBehavior開放Metadata
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using WcfServiceLibrary1;
using System.ServiceModel.Description;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Service is running...");
ServiceHost myHost = new
ServiceHost(typeof(Calc));
myHost.AddServiceEndpoint(typeof(IMyService),
new BasicHttpBinding(),
"http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/");
var metadataBehavior = new ServiceMetadataBehavior();
metadataBehavior.HttpGetEnabled = true;
metadataBehavior.HttpGetUrl = new Uri("http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/mex");
myHost.Description.Behaviors.Add(metadataBehavior);
myHost.Open();
Console.ReadLine();
}
}
}
- CTRL+F5執行
- 在Browser
http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/mex
可以看到WSDL:
3 則留言:
咪咪大大
請問我將wcf佈署到 IIS上面在本機的電腦可以使用,但是在外面網路不能連近來我的電腦。
我有將防火牆 新增連線port 80
但還是只有本機可以使用....
我沒有改到程式,不知道我在操作上面那裏差錯了 !!
client程式的config檔案,有指向真正的Web Server URL嗎?
<client>
<endpoint address="http://你的WebServer真正位址/XXX.svc"
binding="basicHttpBinding" bindingConfiguration="Service"
contract="ServiceReference1.IService" name="Service" />
</client>
張貼留言