Mono中的自托管WCF服务如何实现?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (11)

我目前正在开发一个C#项目,它是一个控制台应用程序,其中包含一个WCF soap服务。

以下是我用来打开主机的代码。

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

这在Windows上工作得很好,但在Linux上,当我浏览到它时,我收到以下消息:

XmlSchema错误:命名项目 http://schemas.microsoft.com/2003/10/Serialization/:anyType已包含在架构对象表中。考虑将MONO_STRICT_MS_COMPLIANT设置为'yes'以模拟MS实现。相关模式项目SourceUri:,第3行,第3位。

我不知道从哪里开始寻找这个问题。

感谢任何帮助,可以提供

根据Rob Goodwins的建议,我已经添加了MONO_STRICT_MS_COMPLIANT环境变量,它似乎已经工作,我可以访问该服务。但是,我现在遇到了一个不同的问题。我有一个PHP页面,每隔几秒就会发出几个请求,但一分钟后,我的应用程序崩溃。

我收到以下异常:

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& 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

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& 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

我似乎也无法通过VS2010 WCF测试客户端访问该服务,但它没有错误,只是在进度条处于50%的位置,并且没有任何进一步的信息。

在Apache错误日志中,我得到以下错误:

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

除了我只能在本地访问服务之外,我似乎现在有这种工作。也就是说,Web服务可以在本地访问,功能可以做他们应该做的事情,但我无法从另一台PC访问它。即我尝试访问Mono服务器上使用WCF测试客户端从我的开发PC的开发PC上的WCF服务,然后我得到以下输出:

Error: Cannot obtain Metadata fromhttp://192.168.1.74:6525/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 athttp://go.microsoft.com/fwlink/?LinkId=65455.WS-MetadataExchange Error URI:http://192.168.1.74:6525/Metadata contains a reference that cannot be resolved:'http://192.168.1.74:6525/'.

Content Type application/soap+xml; charset=utf-8 was not supported by servicehttp://192.168.1.74:6525/. 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:http://192.168.1.74:6525/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 'http://localhost:6525/?xsd=xsd0'. - Unable to connect to the remote server - No connection could be made because the target machine actively refused it 127.0.0.1:6525.

经过进一步测试,看起来好像是一次接入多个调用。在浏览器页面上不停地刷新使用该服务似乎会使其崩溃。

看起来好像System.ServiceModel.Logger.TraceCoreMono内部有什么问题。有没有办法禁用这部分单声道,所以我不受这个问题的影响。

提问于
用户回答回答于

看起来可能有一些来自托管服务器的权限相关问题,请检查一次给予特定端口的权限。希望这可能有用。

扫码关注云+社区