常见的WCF异常:连接意外关闭 [英] Common WCF Exception : Connection Unexpectedly Closed

查看:126
本文介绍了常见的WCF异常:连接意外关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个项目。一个是WCF服务项目,一个是WPF项目,一个是Microsoft单元测试项目。我设置WCF服务项目与一个数据对象,如下:

  [DataContract] 
public enum Priority
{
Low,
中,

}

[DataContract]
public struct TimeInfo
{
[DataMember]
public Int16 EstimatedHours {get;组; }

[DataMember]
public Int16 ActualHours {get;组; }

[DataMember]
public DateTime StartDate {get;组; }

[DataMember]
public DateTime EndDate {get;组; }

[DataMember]
public DateTime CompletionDate {get;组; }
}

[DataContract]
public class Task
{
[DataMember]
public string Title {get;组; }

[DataMember]
public string描述{get;组; }

[DataMember]
public Priority Priority {get;组; }

[DataMember]
public TimeInfo TimeInformation {get;组; }

[DataMember]
public Decimal Cost {get;组; }
}

我的合约如下所示:

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

我得到以下错误:底层连接已关闭:连接意外关闭。 / p>

WPF和单元测试项目的app.config如下所示:

 < system.serviceModel> 
< bindings>
< wsHttpBinding>
< binding name =WSHttpBinding_ITaskManagementcloseTimeout =00:01:00
openTimeout =00:01:00receiveTimeout =00:10:00sendTimeout =00:01: 00
bypassProxyOnLocal =falsetransactionFlow =falsehostNameComparisonMode =StrongWildcard
maxBufferPoolSize =524288maxReceivedMessageSize =65536
messageEncoding =TexttextEncoding =utf-8 useDefaultWebProxy =true
allowCookies =false>
< readerQuotas maxDepth =32maxStringContentLength =8192maxArrayLength =16384
maxBytesPerRead =4096maxNameTableCharCount =16384/>
< reliableSession ordered =trueinactivityTimeout =00:10:00
enabled =false/>
< security mode =Message>
< transport clientCredentialType =WindowsproxyCredentialType =None
realm =/>
< message clientCredentialType =WindowsnegotiateServiceCredential =true
algorithmSuite =DefaultestablishSecurityContext =true/>
< / security>
< / binding>
< / wsHttpBinding>
< / bindings>
< client>
< endpoint address =http:// localhost:9999 / TaskManagement.svc
binding =wsHttpBindingbindingConfiguration =WSHttpBinding_ITaskManagement
contract =TaskManagement.ITaskManagementname = 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 =wsHttpBindingcontract =InternetBasedWcfServices.ITaskManagement>
< identity>
< dns value =localhost/>
< / ident>
< / endpoint>
< endpoint address =mexbinding =mexHttpBindingcontract =IMetadataExchange/>
< / service>
< service behaviorConfiguration =InternetBasedWcfServices.ScheduleManagementBehavior
name =InternetBasedWcfServices.ScheduleManagement>
< endpoint address =binding =wsHttpBindingcontract =InternetBasedWcfServices.IScheduleManagement>
< identity>
< dns value =localhost/>
< / identity>
< / endpoint>
< endpoint address =mexbinding =mexHttpBindingcontract =IMetadataExchange/>
< / service>
< / services>
< /system.serviceModel>

这不是第一次发生这种情况,我猜这是一个配置问题。但每次我通常只是吹走我的服务,把它放回来或创建一个新的服务项目。然后一切工作奇妙。如果任何人有任何想法,那将是真棒。 Thx。



**


更新:
我的疑难解答这个问题。
当有答案时,如果
答案未发布,我将它添加为
官方答案。


**

解决方案

我找到答案



好的,不知道是否kewl回答我自己的问题,但在这里我们去。由于某些原因,枚举需要用[EnumMember]属性标记如下:

  [DataContract] 
public枚举优先级
{
[EnumMember]
低,
[枚举]
中,
[枚举]

}

一旦我这样做,我的测试和服务可以调用没有错误发生。我仍然不知道为什么显示特定的错误。该错误似乎没有对齐与功能原因的错误发生,但这个修复肯定平滑了一切。


I have three projects. One is a WCF Services Project, one is a WPF Project, and one is a Microsoft Unit Testing Project. I setup the WCF Services project with a data object that looks like this:

[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; }
}

My contract looks like this:

[ServiceContract]
public interface ITaskManagement
{
    [OperationContract]
    List<Task> GetTasks();

    [OperationContract]
    void CreateTask(Task taskToCreate);

    [OperationContract]
    void UpdateTask(Task taskToCreate);

    [OperationContract]
    void DeleteTask(Task taskToDelete);
}

When I try to use the service in either the WPF Application or the Unit Test Project with this code:

var client = new TaskManagementClient();

textBox1.Text = client.GetTasks().ToString();

client.Close();

I get the following error: "The underlying connection was closed: The connection was closed unexpectedly."

The app.config for the WPF and Unit Test Projects look like this:

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

and the web.config of the WCF Service looks like this:

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

This is not the first time this has happened, and I'm guessing it is a configuration issue. But each time I've usually just blown away my service and put it back or created a new service project. Then everything works wonderfully. If anyone has any ideas, that would be awesome. Thx.

**

Updated: I've added comments for more of my troubleshooting on this problem. When an answer is available, if the answer is unpublished, I'll add it as an official "answer".

**

解决方案

I Found the Answer

Ok, not sure if it is kewl answering my own question, but here we go. For some reason the enumeration needed to be marked with the [EnumMember] Attributes as below:

[DataContract]
public enum Priority
{
    [EnumMember]
    Low,
    [EnumMember]
    Medium,
    [EnumMember]
    High
}

Once I did that my tests and services could be called without the error occurring. I'm still not sure why that specific error was displayed. The error doesn't seem to align in any correlation with the functional reason the error occurred, but this fix definitely smoothed everything out.

这篇关于常见的WCF异常:连接意外关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆