title: Self hosted WCF service in Mono
body: <p>I am currently working on a C# project which is a console app which has a WCF soap service hosted within it. </p>
  
  <p>Below is the code I am using to open the host.</p>
  
  <pre><code>var baseAddress = new Uri(Configuration.soapServerSettings.soapServerUrl);
  var host = new ServiceHost(typeof(SoapServer), baseAddress);
  BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
  
  var meta = new ServiceMetadataBehavior()
  {
      HttpGetEnabled = true,
      HttpGetUrl = new Uri("", UriKind.Relative)
  };
  
  host.Description.Behaviors.Add(meta);
  
  var debugBehaviour = new ServiceDebugBehavior()
  {
      HttpHelpPageEnabled = true,
      HttpHelpPageUrl = new Uri("", UriKind.Relative),
      IncludeExceptionDetailInFaults = true
  };
  
  ServiceEndpoint endpnt = host.AddServiceEndpoint(
      typeof(ISoapInterface),
      basicHttpBinding,
      "EmailServer");
  
  host.Description.Behaviors.Remove(typeof(ServiceDebugBehavior));
  host.Description.Behaviors.Add(debugBehaviour);
  host.Open();
  </code></pre>
  
  <p>This is working perfectly fine on Windows but on Linux when I get I browse to it I get the following message:</p>
  
  <blockquote>
    <p>XmlSchema error: Named item
    <a href="http://schemas.microsoft.com/2003/10/Serialization/:anyType">http://schemas.microsoft.com/2003/10/Serialization/:anyType</a> was
    already contained in the schema object table. Consider setting
    MONO_STRICT_MS_COMPLIANT to 'yes' to mimic MS implementation. Related
    schema item SourceUri: , Line 3, Position 3.</p>
  </blockquote>
  
  <p>I have no idea where to start looking into this problem. </p>
  
  <p>Thanks for any help you can provide</p>
  
  <p><strong>Update:</strong>
  As per Rob Goodwins suggestion, I've added the <code>MONO_STRICT_MS_COMPLIANT</code> as the environment variable which does seem to have worked I can access the service. However, I am getting a different problem now. I have a PHP page which does several requests every couple of seconds but then after a minute, my app crashes. </p>
  
  <p>I get the following exception:</p>
  
  <blockquote>
    <p>This XmlWriter does not accept StartTag at this state Error.   at
    System.Xml.XmlTextWriter.WriteStartElement (System.String prefix,
    System.String localName, System.String namespaceUri) [0x00000] in
    :0   at
    System.Xml.DefaultXmlWriter.WriteStartElement (System.String prefix,
    System.String localName, System.String ns) [0x00000] in :0   at System.Xml.XmlWriter.WriteStartElement (System.String
    localName, System.String ns) [0x00000] in :0   at
    System.ServiceModel.Logger.TraceCore (TraceEventType eventType, Int32
    id, Boolean hasRelatedActivity, Guid relatedActivity, System.Object[]
    data) [0x00000] in :0   at
    System.ServiceModel.Logger.LogMessage
    (System.ServiceModel.Diagnostics.MessageLogTraceRecord log) [0x00000]
    in :0   at System.ServiceModel.Logger.LogMessage
    (MessageLogSourceKind sourceKind,
    System.ServiceModel.Channels.Message&amp; msg, Int64 maxMessageSize)
    [0x00000] in :0   at
    System.ServiceModel.Channels.Http.HttpRequestContext.InternalReply
    (System.ServiceModel.Channels.Message msg, TimeSpan timeout) [0x00000]
    in :0   at
    System.ServiceModel.Channels.Http.HttpRequestContext.Reply
    (System.ServiceModel.Channels.Message msg, TimeSpan timeout) [0x00000]
    in :0   at
    System.ServiceModel.Dispatcher.MessageProcessingContext.Reply (Boolean
    useTimeout) [0x00000] in :0   at
    System.ServiceModel.Dispatcher.OperationInvokerHandler.Reply
    (System.ServiceModel.Dispatcher.MessageProcessingContext mrc, Boolean
    useTimeout) [0x00000] in :0   at
    System.ServiceModel.Dispatcher.OperationInvokerHandler.ProcessRequest
    (System.ServiceModel.Dispatcher.MessageProcessingContext mrc)
    [0x00000] in :0   at
    System.ServiceModel.Dispatcher.BaseRequestProcessorHandler.ProcessRequestChain
    (System.ServiceModel.Dispatcher.MessageProcessingContext mrc)
    [0x00000] in :0   at
    System.ServiceModel.Dispatcher.BaseRequestProcessorHandler.ProcessRequestChain
    (System.ServiceModel.Dispatcher.MessageProcessingContext mrc)
    [0x00000] in :0   at
    System.ServiceModel.Dispatcher.HandlersChain.ProcessRequestChain
    (System.ServiceModel.Dispatcher.MessageProcessingContext mrc)
    [0x00000] in :0   at
    System.ServiceModel.Dispatcher.BaseRequestProcessor.ProcessRequest
    (System.ServiceModel.Dispatcher.MessageProcessingContext mrc)
    [0x00000] in :0</p>
    
    <p>Unhandled Exception: System.InvalidOperationException: This XmlWriter
    does not accept StartTag at this state Error.   at
    System.Xml.XmlTextWriter.WriteStartElement (System.String prefix,
    System.String localName, System.String namespaceUri) [0x00000] in
    :0   at
    System.Xml.DefaultXmlWriter.WriteStartElement (System.String prefix,
    System.String localName, System.String ns) [0x00000] in :0   at System.Xml.XmlWriter.WriteStartElement (System.String
    localName, System.String ns) [0x00000] in :0   at
    System.ServiceModel.Logger.TraceCore (TraceEventType eventType, Int32
    id, Boolean hasRelatedActivity, Guid relatedActivity, System.Object[]
    data) [0x00000] in :0   at
    System.ServiceModel.Logger.Log (TraceEventType eventType,
    System.String message, System.Object[] args) [0x00000] in :0   at System.ServiceModel.Logger.Error (System.String
    message, System.Object[] args) [0x00000] in :0   at
    System.ServiceModel.Dispatcher.ListenerLoopManager.ProcessErrorWithHandlers
    (IChannel ch, System.Exception ex,
    System.ServiceModel.Channels.Message&amp; res) [0x00000] in :0   at
    System.ServiceModel.Dispatcher.ListenerLoopManager.ProcessRequest
    (IReplyChannel reply, System.ServiceModel.Channels.RequestContext rc)
    [0x00000] in :0   at
    System.ServiceModel.Dispatcher.ListenerLoopManager.TryReceiveRequestDone
    (IAsyncResult result) [0x00000] in :0</p>
  </blockquote>
  
  <p>I also don't seem to be able to access the service via VS2010 WCF Test Client, it doesn't error it just sits there with the progress bar at 50% and doesn't get any further.</p>
  
  <p>In the apache error log I get the following error:</p>
  
  <blockquote>
    <p>System.ArgumentException: Should be something like
    [[hostname:]port:]VPath:realpath   at
    Mono.WebServer.ApplicationServer.AddApplicationsFromCommandLine
    (System.String applications) [0x00000] in :0   at
    (wrapper remoting-invoke-with-check)
    Mono.WebServer.ApplicationServer:AddApplicationsFromCommandLine
    (string)   at Mono.WebServer.Apache.Server.RealMain (System.String[]
    args, Boolean root, IApplicationHost ext_apphost, Boolean quiet)
    [0x00000] in :0   at (wrapper
    remoting-invoke-with-check) Mono.WebServer.Apache.Server:RealMain
    (string[],bool,Mono.WebServer.IApplicationHost,bool)   at
    Mono.WebServer.Apache.Server.Main (System.String[] args) [0x00000] in
    :0 [ERROR] FATAL UNHANDLED EXCEPTION:
    System.ArgumentException: Should be something like
    [[hostname:]port:]VPath:realpath   at
    Mono.WebServer.ApplicationServer.AddApplicationsFromCommandLine
    (System.String applications) [0x00000] in :0   at
    (wrapper remoting-invoke-with-check)
    Mono.WebServer.ApplicationServer:AddApplicationsFromCommandLine
    (string)   at Mono.WebServer.Apache.Server.RealMain (System.String[]
    args, Boolean root, IApplicationHost ext_apphost, Boolean quiet)
    [0x00000] in :0   at (wrapper
    remoting-invoke-with-check) Mono.WebServer.Apache.Server:RealMain
    (string[],bool,Mono.WebServer.IApplicationHost,bool)   at
    Mono.WebServer.Apache.Server.Main (System.String[] args) [0x00000] in
    :0</p>
  </blockquote>
  
  <p><strong>Update 2:</strong>
  I seem to have it kind of working now except I can only access the service locally. I.e. the web service can be accessed locally and the functions do what they should, but I can't access it from another PC. I.e. I am trying to access the WCF service on the Mono server using the WCF test client from my dev PC from dev PC and I am then getting the following output:</p>
  
  <blockquote>
    <p>Error: Cannot obtain Metadata from <code>http://192.168.1.74:6525/</code> If this
    is a Windows (R) Communication Foundation service to which you have
    access, please check that you have enabled metadata publishing at the
    specified address.  For help enabling metadata publishing, please
    refer to the MSDN documentation at
    <a href="http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata">http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata</a> Exchange
    Error    URI: <code>http://192.168.1.74:6525/</code>    Metadata contains a
    reference that cannot be resolved: <code>'http://192.168.1.74:6525/</code>'.<br>
    Content Type application/soap+xml; charset=utf-8 was not supported by
    service <code>http://192.168.1.74:6525/</code>.  The client and service bindings
    may be mismatched.    The remote server returned an error: (415)
    Expected content-type 'text/xml; charset=utf-8' but got
    'application/soap+xml; charset=utf-8'.HTTP GET Error    URI:
    <code>http://192.168.1.74:6525/</code>    The document was understood, but it could
    not be processed.  - The WSDL document contains links that could not
    be resolved.  - There was an error downloading
    '<code>http://localhost:6525/?xsd=xsd0</code>'.  - Unable to connect to the remote
    server  - No connection could be made because the target machine
    actively refused it 127.0.0.1:6525.</p>
  </blockquote>
  
  <p><strong>Update 3:</strong>
  Upon further testing it looks as if when many calls are made into the service in one go. Constantly hiting refresh on the browser page making use of the service seems to crash it. </p>
  
  <p>It looks as if there is something wrong with the <code>System.ServiceModel.Logger.TraceCore</code> within Mono. Is there a way to disable this part of mono so I don't get affected by this issue. </p>