所以我自己托管 WCF 服务。当我通过客户端调用我的 GetProject() 方法时,我得到一个 Unhandled CommuncationException 和一个 InnerException
“底层连接已关闭:接收时发生意外错误。”
还有那个 InnerException:
“无法从传输连接读取数据:现有连接被远程主机强行关闭。”
现在我知道这只是一个一般性的信息,并没有告诉我太多。所以我配置了一个服务跟踪,这就是我得到的:
在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.Reply(MessageRpc& rpc)System.Runtime.Serialization.SerializationException:键入“System.FormatException”,数据合同名称为“FormatException:http: //schemas.datacontract.org/2004/07/系统' 不是预期的。考虑使用 DataContractResolver 或将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute 属性或将它们添加到传递给 DataContractSerializer 的已知类型列表中。
所以我接受了这条消息并修改了我的代码,我相信我现在这样做是正确的。我已经学习了几个教程,但我仍然在服务跟踪中不断收到这个异常。
在我的ServiceContract
我有以下内容:
[OperationContract]
Project GetProject(int id);
编辑删除KnownTypeAttribute
使用。在此处注释以反映更改。
这是我对 Project 类及其使用的类的实现:
[DataContract]
//[KnownType(typeof(Contact))]
//[KnownType(typeof(ProjectDetails))]
//[KnownType(typeof(CommercialEntity))]
public class Project
{
[DataMember]
public ProjectDetails details = new ProjectDetails();
[DataMember]
public List<Contact> contacts = new List<Contact>();
[DataMember]
public List<CommercialEntity> POs = new List<CommercialEntity>();
[DataMember]
public List<CommercialEntity> invoices = new List<CommercialEntity>();
[DataMember]
public List<CommercialEntity> quotes = new List<CommercialEntity>();
[DataMember]
public List<CommercialEntity> CRs = new List<CommercialEntity>();
}
[DataContract]
public class ProjectDetails
{
[DataMember]
public int projectId { get; set; }
[DataMember]
public string name { get; set; }
[DataMember]
public int calYearId { get; set; }
[DataMember]
public string projectState { get; set; }
[DataMember]
public string reportTitle { get; set; }
[DataMember]
public int plantId { get; set; }
[DataMember]
public string holdNumber { get; set; }
[DataMember]
public string holdDescription { get; set; }
[DataMember]
public string remarks { get; set; }
[DataMember]
public string adminComments {get; set; }
[DataMember]
public int companyId { get; set; }
[DataMember]
public int customerId { get; set; }
[DataMember]
public string folderNumber { get; set; }
[DataMember]
public int invoicedInFull { get; set; }
[DataMember]
public int approved { get; set; }
}
}
[DataContract]
public class CommercialEntity
{
[DataMember]
public string number { get; set; }
[DataMember]
public DateTime date { get; set; }
[DataMember]
public string comments { get; set; }
public CommercialEntity(string setNumber, DateTime setDate, string setComments)
{
number = setNumber;
date = setDate;
comments = setComments;
}
}
[DataContract]
public class Contact
{
[DataMember]
public string firstName { get; set; }
[DataMember]
public string lastName { get; set; }
[DataMember]
public string emailAddress { get; set; }
[DataMember]
public string phoneNumber { get; set; }
[DataMember]
public int alert { get; set; }
[DataMember]
public int projectLead { get; set; }
[DataMember]
public int mainContact { get; set; }
public Contact(string first, string last, string email, string phone, int alrt, int projLead, int mainCntct)
{
firstName = first;
lastName = last;
emailAddress = email;
phoneNumber = phone;
alert = alrt;
projectLead = projLead;
mainContact = mainCntct;
}
public Contact() { }
}
我的理解的漏洞在哪里?
这是我运行客户端时看到的内容:
有关调用即时 (JIT) 调试而不是此对话框的详细信息,请参阅此消息的末尾。
** * **异常文本** * ****
System.ServiceModel.CommunicationException:接收对 myInternalIP:myPort/ProjectService/ProjectService 的 HTTP 响应时出错。这可能是由于服务端点绑定未使用 HTTP 协议。这也可能是由于服务器中止了 HTTP 请求上下文(可能是由于服务关闭)。有关更多详细信息,请参阅服务器日志。---> System.Net.WebException:底层连接已关闭:接收时发生意外错误。---> System.IO.IOException: Unable to read data from the transport connection: 一个现有的连接被远程主机强行关闭。---> System.Net.Sockets.SocketException: 现有连接被远程主机强行关闭
在 System.Net.Sockets.Socket.Receive(Byte[] 缓冲区,Int32 偏移量,Int32 大小,SocketFlags socketFlags)
在 System.Net.Sockets.NetworkStream.Read(字节 [] 缓冲区,Int32 偏移量,Int32 大小)
--- 内部异常堆栈跟踪结束 ---
在 System.Net.Sockets.NetworkStream.Read(字节 [] 缓冲区,Int32 偏移量,Int32 大小)
在 System.Net.PooledStream.Read(字节 [] 缓冲区,Int32 偏移量,Int32 大小)
在 System.Net.Connection.SyncRead(HttpWebRequest 请求,布尔 userRetrievedStream,布尔 probeRead)
--- 内部异常堆栈跟踪结束 ---
在 System.Net.HttpWebRequest.GetResponse()
在 System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan 超时)
--- 内部异常堆栈跟踪结束 ---
服务器堆栈跟踪:
在 System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)在 System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan 超时)
在 System.ServiceModel.Channels.RequestChannel.Request(消息消息,TimeSpan 超时)
在 System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息消息,TimeSpan 超时)
在 System.ServiceModel.Channels.ServiceChannel.Call(字符串操作,布尔单向,ProxyOperationRuntime 操作,Object[] 输入,Object[] 输出,TimeSpan 超时)
在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime 操作)
在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage 消息)
在 [0] 处重新抛出异常:
在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg)
在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData,Int32 类型)
在 IProjectService.GetQuerytoList(Int32 id)
在 C:\Users\drwill\documents\visual studio 2010\Projects\ProjectBaseWCF\ProjectBase\generatedProxy.cs 中的 ProjectServiceClient.GetQuerytoList(Int32 id):C
中 ProjectWCFClient.MainForm.openToolStripMenuItem_Click(Object sender, EventArgs e) 的第 1065 行: \Users\drwill\documents\visual studio 2010\Projects\ProjectBaseWCF\ProjectBase\PBMain.cs:line 961在 C:\Users\drwill\documents\visual studio 2010\Projects\ProjectBaseWCF\ProjectBase\PBMain.cs:line 55 中的 ProjectWCFClient.MainForm.ProjectListBox_DoubleClick(Object sender, EventArgs e)
在 System.Windows.Forms.Control.OnDoubleClick(EventArgs e)
在 System.Windows.Forms.ListBox.WndProc(消息和 m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和 m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)
这是我的客户端配置:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IProjectService" maxReceivedMessageSize="500000000" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://192.168.0.99:9000/ProjectService/ProjectService"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IProjectService"
contract="IProjectService" name="BasicHttpBinding_IProjectService" />
</client>
</system.serviceModel>
</configuration>
这是我的服务配置:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true"/>
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service behaviorConfiguration="ProjectBaseWCFServiceLib.Service1Behavior" name="ProjectBaseWCFServiceLib.ProjectService">
<endpoint address="" binding="basicHttpBinding" contract="ProjectBaseWCFServiceLib.IProjectService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/ProjectBaseWCFServiceLib/Service1/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ProjectBaseWCFServiceLib.Service1Behavior">
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<dataContractSerializer maxItemsInObjectGraph="2147483646"/>
<serviceDebug includeExceptionDetailInFaults="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
此外,我以编程方式配置我的控制台应用程序主机:
BasicHttpBinding binding = new BasicHttpBinding();
binding.OpenTimeout = new TimeSpan(0, 10, 0);
binding.CloseTimeout = new TimeSpan(0, 10, 0);
binding.SendTimeout = new TimeSpan(0, 10, 0);
binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
binding.MaxReceivedMessageSize = 500000000;
binding.MaxBufferSize = 500000000;
binding.MaxBufferPoolSize = 500000000;
selfHost.AddServiceEndpoint(typeof(IProjectService), binding, "ProjectService");