我對WCF中的託管服務存在一些疑問。瞭解使用ServiceHost類及其構造函數的WCF自託管方法
我想使用自託管方法託管服務。 ServiceHost
班來救援。 通過使用它,可以託管可以直接訪問Windows主機框架的服務。 好了,請考慮以下方法:
0)共同假設:所有病例假設配置文件App.config
是爲了設置終點ABC使用。所以在下面的代碼中沒有提到端點,只是不打擾它。 我們會考慮這項服務太:
[ServiceContract]
public interface IMyService {
[OperationContract]
string MyOp1(int myint);
[OperationContract]
string MyOp2(int myint);
}
public class MyService : IMyService {
// This service needs to be constructed providing at least a string or an integer, if an attempt to construct it wothout passing any of these is done, the service should raise an error.
MyService(string strparam) { ... }
MyService(int intparam) { ... }
MyService(string strparam, int intparam) { ... }
public string MyOp1(int myint) { ... }
public string MyOp2(int myint) { ... }
}
public class MyStandaloneService : IMyService {
// This service does not need to be constructed.
MyStandaloneService() { ... }
public string MyOp1(int myint) { ... }
public string MyOp2(int myint) { ... }
}
1)案例1:有可能使用ServiceHost
類的此重載託管服務:
public ServiceHost(
Type serviceType,
params Uri[] baseAddresses
)
通過使用它,它是可能讓框架管理服務實例,因爲只需要服務類型。當然,如果服務是建設是基礎建設。 no參數構造函數將被調用。
using (ServiceHost host = new ServiceHost(typeof(MyStandaloneService))) {
host.Open();
...
host.Close();
}
2)情形2:處理不需要特殊的施工服務...某種獨立的服務時,該超載好有可能主辦使用ServiceHost
類的此重載服務:
public ServiceHost(
Object singletonInstance,
params Uri[] baseAddresses
)
通過使用它,就可以實例化一個服務,然後主持它沒有讓框架處理這個...這個方法有需要特殊處理,並沒有完全獨立的服務打交道時是好的:
MyService MS = new MyService("the string");
using (ServiceHost host = new ServiceHost(MS)) {
host.Open();
...
host.Close();
}
那麼我想了解以下信息:
A)在CASE 1也能夠通過提供的類型自動託管服務。如果我嘗試創建另一個相同類型的服務(MyStandaloneService
),是否會因爲嘗試創建兩個相同的服務而導致錯誤?可能我應該硬編碼,在這種情況下,端點配置,因爲使用配置文件將導致兩個相同的服務託管在同一地址。
B)在CASE 2 MSDN文檔說這會創建一個單例服務實例。所以如果我嘗試以這種方式託管另一項服務:
MyService MS = new MyService("the string");
MyService MS2 = new MyService(23);
ServiceHost host = new ServiceHost(MS));
ServiceHost host2 = new ServiceHost(MS2));
host.Open();
host2.Open();
...
host.Close();
host2.Close();
我會收到錯誤嗎?
C)如果我想避免單例實例,我應該怎麼做?
感謝