2011-09-27 85 views
2

我正在仔细地踏入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(); 
     } 
    } 
+1

您是否意外地定义了两次构造函数?你确定你不是以某种方式使用定义第二个构造函数的代码生成吗? [根据此问题](http://stackoverflow.com/questions/381831/can-wcf-service-have-constructors),您可以在WCF服务中定义构造函数,以便错误是合法的,而不是WCF中的缺陷。 –

回答

4

这是没有关系的存在的构造函数。我确信这是一个复制/粘贴错误 - 在应用程序的任何文件中搜索单词“FileService”,并且您将找到具有该名称的另一个类或名称空间声明(在相同的名称空间中)。

+0

谢谢。我认为这是我的菜鸟手指搞乱从我的asmx项目复制/粘贴到模板创建的项目。 –

2

有些事情可以做:

  • 做单击鼠标右键>查找的FileService引用。
  • 尝试完整搜索(ctrl + f)并搜索FileService。
  • 检查第二个构造函数的任何部分类。
  • 尝试清洁解决方案,然后重新构建解决方案,看看这是否使 有任何区别。
  • ...