我有三个项目。一个是WCF服务项目,一个是WPF项目,还有一个是Microsoft单元测试项目。我使用一个数据对象来设置WCF服务项目,它看起来像这样:
[DataContract]
public enum Priority
{
Low,
Medium,
High
}
[DataContract]
public struct TimeInfo
{
[DataMember]
public Int16 EstimatedHours { get; set; }
[DataMember]
public Int16 ActualHours { get; set; }
[DataMember]
public DateTime StartDate { get; set; }
[DataMember]
public DateTime EndDate { get; set; }
[DataMember]
public DateTime CompletionDate { get; set; }
}
[DataContract]
public class Task
{
[DataMember]
public string Title { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public Priority Priority { get; set; }
[DataMember]
public TimeInfo TimeInformation { get; set; }
[DataMember]
public Decimal Cost { get; set; }
}
我的合同看起来像这样:
[ServiceContract]
public interface ITaskManagement
{
[OperationContract]
List<Task> GetTasks();
[OperationContract]
void CreateTask(Task taskToCreate);
[OperationContract]
void UpdateTask(Task taskToCreate);
[OperationContract]
void DeleteTask(Task taskToDelete);
}
当我尝试通过此代码在WPF应用程序或单元测试项目中使用服务时:var client = new TaskManagementClient();
textBox1.Text = client.GetTasks().ToString();
client.Close();
我收到以下错误:"底层连接已关闭:连接意外关闭。"
WPF和单元测试项目的app.config如下所示:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_ITaskManagement" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:9999/TaskManagement.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITaskManagement"
contract="TaskManagement.ITaskManagement" name="WSHttpBinding_ITaskManagement">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
WCF服务的web.config如下:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="InternetBasedWcfServices.TaskManagementBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior name="InternetBasedWcfServices.ScheduleManagementBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="InternetBasedWcfServices.TaskManagementBehavior"
name="InternetBasedWcfServices.TaskManagement">
<endpoint address="" binding="wsHttpBinding" contract="InternetBasedWcfServices.ITaskManagement">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
<service behaviorConfiguration="InternetBasedWcfServices.ScheduleManagementBehavior"
name="InternetBasedWcfServices.ScheduleManagement">
<endpoint address="" binding="wsHttpBinding" contract="InternetBasedWcfServices.IScheduleManagement">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
这已经不是第一次发生了,我猜测这是一个配置问题。但每次通常都只是删除我的服务并重新创建或者创建一个新的服务项目。然后一切都很完美。如果有人有任何想法,那就太棒了。谢谢。
**
更新:我已经添加了更多的排除故障信息。当有答案可用时,如果答案未发布,我将把它作为官方“答案”添加。
**