我在C#中运行ServiceHost(WCF)的Open函数时出现System.Xml.XmlException

时间:2016-02-21 01:46:15

标签: c# wcf

当我将代码从一台计算机(A)移动到另一台计算机(B)时,我遇到了问题。问题是当代码运行ServiceHost的Open函数时,我得到以下异常,尽管仍然可以打开服务。换句话说,此异常不会影响服务。计算机A中不会发生此问题。

Exception thrown: 'System.Xml.XmlException' in System.Xml.dll
Exception thrown: 'System.Xml.XmlException' in System.Xml.dll
Exception thrown: 'System.Xml.XmlException' in System.Xml.dll
Exception thrown: 'System.Xml.XmlException' in System.Xml.dll
Exception thrown: 'System.Xml.XmlException' in System.Xml.dll
Exception thrown: 'System.Xml.XmlException' in System.Xml.dll
Exception thrown: 'System.Xml.XmlException' in System.Xml.dll
Exception thrown: 'System.Xml.XmlException' in System.Xml.dll
Exception thrown: 'System.Xml.XmlException' in System.Xml.dll
Exception thrown: 'System.Xml.XmlException' in System.Xml.dll
Exception thrown: 'System.Xml.XmlException' in System.Xml.dll
Exception thrown: 'System.Xml.XmlException' in System.Xml.dll
Exception thrown: 'System.Xml.XmlException' in System.Xml.dll

我创建了自己的TRFServiceHost,它来自ServiceHost,如下所示

public class TRFServiceHost : ServiceHost
{
    private string _configureFilePath;

    public TRFServiceHost(Type serviceType, params Uri[] baseAddresses) : base(serviceType, baseAddresses)
    {
    }

    protected override void ApplyConfiguration()
    {
        this._configureFilePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase +
                                  "TRFServerModule.config";
        if (!File.Exists(this._configureFilePath))
        {
            base.ApplyConfiguration();
            return;
        }
        var configFileMap = new ExeConfigurationFileMap { ExeConfigFilename = this._configureFilePath };
        var config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap,
            ConfigurationUserLevel.None);
        var serviceModel = ServiceModelSectionGroup.GetSectionGroup(config);
        if (serviceModel == null)
        {
            base.ApplyConfiguration();
            return;
        }
        foreach (ServiceElement serviceElement in serviceModel.Services.Services)
        {
            this.LoadConfigurationSection(serviceElement);
            return;
        }
    }
}

然后我将此课程称为如下:

try
        {
            if (this._trfServiceHost == null)
                this._trfServiceHost = new TRFServiceHost(typeof(TRFServerService));

            if (this._trfServiceHost.State != CommunicationState.Opened)
            {
                this._trfServiceHost.Open();
                message = "TRF Service has been turned on.";
            }
        }
        catch (Exception e)
        {
            message = "TRF Service cannot be turned on. " + e.Message;
        }

配置文件如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
  <netTcpBinding>
    <binding sendTimeout="00:10:00" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
      <security mode="None"></security>
    </binding>
  </netTcpBinding>
</bindings>
<services>
  <service name="TRFServerModule.TRFServerService">
    <endpoint address="" binding="netTcpBinding" contract="TRFCommonLibs.ITRFService"></endpoint>
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://127.0.0.1:4530"/>
      </baseAddresses>
    </host>
  </service>
</services>

谢谢。

0 个答案:

没有答案