我的 Windows 服务现在显示了我的 WCF 实现类。因此,当我启动我的服务时,它给了我“ServiceHost 仅支持类服务类型”的错误。这是我的代码:
try
{
Console.WriteLine("Testing 1");
WCFEmailEventLog.WriteEntry("My service started");
System.Diagnostics.Debugger.Break();
// Create a ServiceHost
// provide the base address.
serviceHost = new ServiceHost(typeof(TestBulkEmailService.BulkEmailService), new Uri("http://localhost/TestBulkEmailService/TestBulkEmailService.svc") );
WCFEmailEventLog.WriteEntry("After Service host ");
// Open the ServiceHostBase to create listeners and start
// listening for messages.
serviceHost.Open();
在上面的代码中,typeOf(TestBulkEmailService.BulkEmailService) 不显示实现类 BulkEmailService,只显示我的 WCF 合同 IBulkEmailService。
我究竟做错了什么?
这是我的 app.config 文件:
<services>
<service name="WW.Common.Service.Impl.BulkEmailService" behaviorConfiguration="BulkEmailService">
<host>
<baseAddresses>
<add baseAddress="http://localhost/TestBulkEmailService/TestBulkEmailService.svc"/>
</baseAddresses>
</host>
<endpoint address="BulkEmailService" binding="basicHttpBinding" contract="TestBulkEmailService.IBulkEmailService" />
<endpoint address="mex" binding="basicHttpBinding" name="mexEndpoint" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="BulkEmailService">
<serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>
<serviceMetadata httpGetEnabled="true" />
<serviceSecurityAudit auditLogLocation="Application"
suppressAuditFailure="true"
serviceAuthorizationAuditLevel="Success"
messageAuthenticationAuditLevel="Success" />
</behavior>
</serviceBehaviors>
</behaviors>
这是我的 .svc 文件,它托管在 IIS 中,在使用客户端应用程序时运行良好。
<%@ ServiceHost Language="C#" Debug="true" Service="WW.Common.Service.Impl.BulkEmailService" %>
这是我用作服务参考并将其命名为 TestBulkEmailService 的 WCF 服务的实现和合同。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using WW.EnterpriseLibrary.Error;
using WW.Common.Service.Contract;
using WW.Common.Data.DTO;
using WW.Common.Data;
using WW.ServiceCore.Error;
using WW.ServiceCore.Log;
using BulkEmailDac = WW.Common.DataAccess.Impl.BulkEmailDac;
namespace WW.Common.Service.Impl
{
public class BulkEmailService : IBulkEmailService
{
private BulkEmailDac emailDac;
ErrorMsg err_msg = new ErrorMsg();
public BulkEmailService()
{
emailDac = new BulkEmailDac();
}
public string Abc(string s1)
{
string result = s1;
return result;
}
/// <summary>
/// Get Bulk Email Record List
/// </summary>
/// <returns></returns>
public BulkEmailDTOList GetBulkEmailInfo(int recordLimit)
{
try
{
return emailDac.GetBulkEmailInfo(recordLimit);
}
catch (Exception ex)
{
if (ex is FaultException<OperationFault>)
{
//throw;
Console.WriteLine("faultException1 Message: " + ex.Message);
throw;
}
else
{
//LOG AND THROW AN UNKNOWN EXCEPTION
ApplicationLog.WriteError(DateTime.Now.ToString() + "|"
+ "GetBulkEmailInfo" + "|"
+ ex.Message + "|"
+ ex.StackTrace);
Console.WriteLine("faultException2 Message: " + ex.StackTrace);
throw new FaultException<OperationFault>(new OperationFault(ex.Message, ErrorMessages.Unknown_Error_Code));
}
}
}
合同:
using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using WW.EnterpriseLibrary.Error;
using WW.Common.Data.DTO;
namespace WW.Common.Service.Contract
{
[ServiceContract]
public interface IBulkEmailService
{
[OperationContract]
[FaultContractAttribute(typeof(OperationFault))]
BulkEmailDTOList GetBulkEmailInfo(int recordLimit);
[OperationContract]
[FaultContractAttribute(typeof(OperationFault))]
int SendBulkEmail(string emailFm, string emailTo, string ccTo, string subject, string body);
}