wcf可以引用COM組件,并且開啟服務,讓遠程計算機調用嗎?
我做了個例子引用了Com組件,并遠程調用里面的方法.
可是程序報錯.
[System.ServiceModel.CommunicationObjectFaultedException] = {"通信對象 System.ServiceModel.ServiceHost 無法用于通信,因為其處于“出錯”狀態。"}
?
這個是契約
[ServiceContract(Name = "CalculatorService", Namespace = "http://www.artech.com/")]?? ?public interface ICalculator?? ?{ ? ? ? ?
#region 聯機?? ? ??
?[OperationContract]?? ? ? ?bool Connect();?? ? ? ?
#endregion
}
?
這個是實體方法
public class CalculatorService : ICalculator?? ?{?? ? ? ?
#region 全局變量 ? ? ? ?
static CommHelper devClient;?? ? ? ?#endregion
#region 聯機?public bool Connect()?? ? ? ?{?? ? ? ? ? ?
devClient = new CommHelper();?? ? ? ? ? ?
devClient.ReceiverID = @"TS0STUVWXYZ[\]";?? ? ? ? ??
?devClient.TestingTime = 30000;?? ? ? ? ? ?
return devClient.ConnectDev("192.168.1.234", 60000, 1);?? ? ? ?
}?? ? ? ?
#endregion
}
?
這個控制臺程序
using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))?? ? ? ? ? ? ? ?{?? ? ? ? ? ? ? ? ? ?host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://127.0.0.1:9999/CalculatorService");?? ? ? ? ? ? ? ? ? ?if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)?? ? ? ? ? ? ? ? ? ?{?? ? ? ? ? ? ? ? ? ? ? ?ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();?? ? ? ? ? ? ? ? ? ? ? ?behavior.HttpGetEnabled = true;?? ? ? ? ? ? ? ? ? ? ? ?behavior.HttpGetUrl = new Uri("http://127.0.0.1:9999/CalculatorService/metadata");?? ? ? ? ? ? ? ? ? ? ? ?host.Description.Behaviors.Add(behavior);?? ? ? ? ? ? ? ? ? ?}?? ? ? ? ? ? ? ? ? ?host.Opened += delegate?? ? ? ? ? ? ? ? ? ?{?? ? ? ? ? ? ? ? ? ? ? ?Console.WriteLine("CalculaorService已經啟動,按任意鍵終止服務!");?? ? ? ? ? ? ? ? ? ?};?? ? ? ? ? ? ? ? ? ?host.Open();?? ? ? ? ? ? ? ? ? ?Console.Read();?? ? ? ? ? ? ? ?}
wcf引用com組件
慕碼人2483693
2018-12-07 05:25:38