我正在仔细地踏入WCF尝试遵循教程并将我的ASMX项目转换为新的WCF项目,并且我偶然发现了一个关于编码WCF中的构造函数。c#命名空间已经包含了继承类的定义,所以如何添加构造函数
我ASMX web服务让我有一个构造函数(见:同名,无返回值):
namespace sdkTrimFileServiceASMX
{
public class FileService : System.Web.Services.WebService
{
Database db;
string g_EventSource = "CBMI-TrimBroker";
string g_EventLog = "Application";
public FileService()
{
try
{
if (!EventLog.SourceExists(g_EventSource))
EventLog.CreateEventSource(g_EventSource, g_EventLog);
}
catch (InvalidOperationException e)
{
e.ToString();
}
}
我尝试将其转换为一个WCF服务应用程序,让该编译器抱怨:
The namespace 'sdkTRIMFileServiceWCF' already contains a definition for 'FileService'
这里是WCF代码(开头段):
namespace sdkTRIMFileServiceWCF
{
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class FileService : IFileService // err msg points to this line
{
Database db;
string g_EventSource = "CBMI-TrimBroker";
string g_EventLog = "Application";
public FileService()
{
try
{
if (!EventLog.SourceExists(g_EventSource))
EventLog.CreateEventSource(g_EventSource, g_EventLog);
}
catch (InvalidOperationException e)
{
e.ToString();
}
}
您是否意外地定义了两次构造函数?你确定你不是以某种方式使用定义第二个构造函数的代码生成吗? [根据此问题](http://stackoverflow.com/questions/381831/can-wcf-service-have-constructors),您可以在WCF服务中定义构造函数,以便错误是合法的,而不是WCF中的缺陷。 –