c#命名空间已经包含了继承类的定义,所以如何添加构造函数

本文关键字:何添加 构造函数 添加 定义 命名空间 包含 继承 | 更新日期: 2023-09-27 18:09:44

我正在小心翼翼地进入WCF,试图遵循教程并将我的ASMX项目转换为新的WCF项目,我偶然发现了一个关于在WCF中编写构造函数的奥秘。

我的ASMX webservice允许我有一个构造函数(参见:同名,无返回值):

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();
        }
    }

c#命名空间已经包含了继承类的定义,所以如何添加构造函数

这与构造函数的存在无关。我很确定这是一个复制/粘贴错误-在应用程序中的任何文件中搜索单词"FileService",您会发现另一个类或具有该名称的命名空间声明(在同一命名空间中)。

你可以做的事情:

  • 做鼠标右键>查找文件服务的引用。
  • 尝试完整搜索(ctrl+f)并搜索FileService
  • 检查任何部分类是否有第二个构造函数。
  • 尝试干净的解决方案,然后重建解决方案,看看这是否使任何作用。