我有一个应该托管服务的应用程序。该服务正在实施Interface
喜欢这个:
[ServiceContract]
public interface IRegistrationCaseFetcherService
{
[OperationContract]
bool CancelTransaction(string transactionControlNumber);
[OperationContract]
bool ConfirmTransaction(string transactionControlNumber);
[OperationContract]
bool UpdateTransaction(string transactionControlNumber, UpdatedRegistrationCase personData);
[OperationContract]
bool checkEndPoint();
}
我的服务定义如下:
[ServiceBehavior(UseSynchronizationContext = false)]
public class RegistrationCaseFetcherService : IRegistrationCaseFetcherService
{
static int CallsInProgress = 0;
public bool CancelTransaction(string transactionControlNumber)
{
return true;
}
public bool ConfirmTransaction(string transactionControlNumber)
{
return true;
}
public bool UpdateTransaction(string transactionControlNumber, UpdatedRegistrationCase personData)
{
return true;
}
public bool checkEndPoint()
{
return true;
}
public static bool IsInUse
{
get
{
return (CallsInProgress != 0);
}
}
}
我也有一个类来触发这项服务:
public static class Services
{
private static ServiceHost _service;
private static bool _isHosted = false;
public static bool StartRecieveService()
{
var address = string.Format("http://{0}:8732/Services/RService", Dns.GetHostName());
_service = new ServiceHost(typeof(RegistrationCaseFetcherService), new Uri(address));
_service.AddServiceEndpoint(typeof(IRegistrationCaseFetcherService), new BasicHttpBinding(), address);
var serviceMetaDataBehaviour = _service.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (serviceMetaDataBehaviour == null) serviceMetaDataBehaviour = new ServiceMetadataBehavior();
serviceMetaDataBehaviour.HttpGetEnabled = true;
serviceMetaDataBehaviour.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
_service.Description.Behaviors.Add(serviceMetaDataBehaviour);
try
{
_service.Open();
_isHosted = true;
if (_service.BaseAddresses.Count > 0)
_logger.Trace(String.Format("Waiting for incomming calls on: {0}", _service.BaseAddresses[0].AbsoluteUri));
return true;
}
catch (Exception e)
{
_logger.Error("Failed with exception: " + e);
return false;
}
}
}
我调试时似乎正在触发服务,不会抛出任何异常。通过在我的Web浏览器中打开URL无法正常工作,它表示资源不存在。我也尝试在WCF Test Client
中打开它,但后来我得到Error: Cannot Obtain Metadata
。
如何才能真正验证我的服务已启动并正常运行?我的代码应该做些什么改变才能让它工作?
提前致谢
答案 0 :(得分:2)
看看
您需要添加至少一个端点(此处为http为&#34; wsdl&#34;),其中公开了元数据:
_service.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName,
MetadataExchangeBindings.CreateMexHttpBinding(),
"mex");