剛開始學習WCF﹐依照書上的範例﹐寫一個裝載務的應用程式。
其中在ServiceHost這段有個疑問App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="Products.ProductsService" behaviorConfiguration="ProductsBehavior">
<endpoint address="http://localhost:8000/ProductsService/ProductsService.svc"
binding="basicHttpBinding"
contract="Products.IProductsService"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ProductsBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>HostController.xaml.cs
namespace ProductsServiceHost {
public partial class HostController : Window {
private ServiceHost productsServiceHost;
//啟動服務
private void Start_Click(object sender, RoutedEventArgs e) {
productsServiceHost = new ServiceHost(typeof(ProductsService));
//productsServiceHost = new ServiceHost(typeof(ProductsService), new System.Uri("http://localhost:8000/ProductsService/ProductsService.svc"));
productsServiceHost.Open();
stop.IsEnabled = true;
start.IsEnabled = false;
status.Text = "Service Running";
} ....
}
}在上述的App.config的endpoint中己設定了address﹐
但我依著書上在Start_Click中一樣是寫著productsServiceHost = new ServiceHost(typeof(ProductsService));
程式可以compiler﹐但執行時一旦open()就出現錯誤﹐錯誤的訊息大意是說沒有提供http起始位址。我查ServiceHost的建構式應該是兩個參數ServiceHost(Type, Uri[])﹐所以我若把程式換成
productsServiceHost = new ServiceHost(typeof(ProductsService), new System.Uri("http://localhost:8000/ProductsService/ProductsService.svc"));
在open時就沒有問題。
可是﹐我查了網路的一些範例都是寫 productsServiceHost = new ServiceHost(typeof(ProductsService)); 這樣子﹐而者在app.config中己有設定了位址﹐
不應該在程式中直接寫死位址﹐請問我是有漏了什麼嗎?我使用的是vs 2008﹐書的範例是使用vs 2005﹐會有差別嗎?
其中在ServiceHost這段有個疑問App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="Products.ProductsService" behaviorConfiguration="ProductsBehavior">
<endpoint address="http://localhost:8000/ProductsService/ProductsService.svc"
binding="basicHttpBinding"
contract="Products.IProductsService"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ProductsBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>HostController.xaml.cs
namespace ProductsServiceHost {
public partial class HostController : Window {
private ServiceHost productsServiceHost;
//啟動服務
private void Start_Click(object sender, RoutedEventArgs e) {
productsServiceHost = new ServiceHost(typeof(ProductsService));
//productsServiceHost = new ServiceHost(typeof(ProductsService), new System.Uri("http://localhost:8000/ProductsService/ProductsService.svc"));
productsServiceHost.Open();
stop.IsEnabled = true;
start.IsEnabled = false;
status.Text = "Service Running";
} ....
}
}在上述的App.config的endpoint中己設定了address﹐
但我依著書上在Start_Click中一樣是寫著productsServiceHost = new ServiceHost(typeof(ProductsService));
程式可以compiler﹐但執行時一旦open()就出現錯誤﹐錯誤的訊息大意是說沒有提供http起始位址。我查ServiceHost的建構式應該是兩個參數ServiceHost(Type, Uri[])﹐所以我若把程式換成
productsServiceHost = new ServiceHost(typeof(ProductsService), new System.Uri("http://localhost:8000/ProductsService/ProductsService.svc"));
在open時就沒有問題。
可是﹐我查了網路的一些範例都是寫 productsServiceHost = new ServiceHost(typeof(ProductsService)); 這樣子﹐而者在app.config中己有設定了位址﹐
不應該在程式中直接寫死位址﹐請問我是有漏了什麼嗎?我使用的是vs 2008﹐書的範例是使用vs 2005﹐會有差別嗎?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货