首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >返回数据的WCF CommunicationException

返回数据的WCF CommunicationException
EN

Stack Overflow用户
提问于 2011-03-10 08:34:51
回答 2查看 2.3K关注 0票数 3

在这里,我们已经开发了一个可以从外部到达的SOAP。因为API的需求之一发生了变化,所以我想向这个API中添加一个新的类,以便为某些函数调用生成正确的路径。

我们的API分为三个独立的库:

  • 一个用于对象
  • 一个用于接口
  • 一项用于执行。

当然,客户端可以在脚本中使用前两个,服务器拥有所有这三个。

我希望添加到API中的类如下所示:

代码语言:javascript
复制
namespace TenForce.Execution.API.Objects.Helpers
{
/// <summary>
/// <para>This interface defines the functionality available in the PathHelper for the API.</para>
/// </summary>
public interface IPathHelper
{
    string ApplicationFolder { get; }   // The HomeDataFolder for the application
    string CompanyHomeFolder { get; }   // The HomeDataFolder for the company.
    string CustomFolder { get; }        // The custom folder for professional services.
    string WikiFolder { get; }          // The WIKI folder to store pages.
    string AddinsFolder { get; }        // The AddinFolder to access the addins.
}
}

实际的类实现如下所示:

代码语言:javascript
复制
using System.IO;
using System.Runtime.Serialization;
using TenForce.Execution.BUL;
using TenForce.Execution.Framework;

namespace TenForce.Execution.API.Implementation.Helpers
{
/// <summary>
/// <para>This class provides a direct implementation of the IPathHelper for the API implementation
/// and manages all the paths inside the DataHomeFolder structure for the TenForce application.</para>
/// </summary>
[DataContract]
public class PathHelper : Objects.Helpers.IPathHelper
{
    #region Private Fields

    private readonly ParameterBUL _mParameterBul;
    private const Parameter.ParameterId DataHomeFolderId = Parameter.ParameterId.DataHomeFolder;
    private const Parameter.ParameterId CompanyNameId = Parameter.ParameterId.CompanyName;

    #endregion

    #region Constructor

    /// <summary>
    /// <para>Creates a new instance of the PathHelper class</para>
    /// </summary>
    public PathHelper()
    {
        _mParameterBul = new ParameterBUL();
    }

    #endregion

    #region IPathHelper Members

    /// <summary>
    /// <para>Returns the absolute path to the DataHomeFolder of the TenForce Application.</para>
    /// </summary>
    [DataMember]
    public string ApplicationFolder
    {
        get
        {
            return CreatePath(_mParameterBul.GetParameterValue(DataHomeFolderId));
        }
    }

    /// <summary>
    /// <para>Returns the absolute path to the Company DataHomeFolder.</para>
    /// </summary>
    [DataMember]
    public string CompanyHomeFolder
    {
        get
        {
            return CreatePath(Path.Combine(ApplicationFolder, _mParameterBul.GetParameterValue(CompanyNameId)));
        }
    }

    /// <summary>
    /// <para>Returns the absolute path to the Company custom folder.</para>
    /// </summary>
    [DataMember]
    public string CustomFolder
    {
        get
        {
            return CreatePath(Path.Combine(CompanyHomeFolder, @"custom"));
        }
    }

    /// <summary>
    /// <para>Returns the absolute path to the Company wiki folder.</para>
    /// </summary>
    [DataMember]
    public string WikiFolder
    {
        get
        {
            return CreatePath(Path.Combine(CompanyHomeFolder, @"wiki"));
        }
    }

    /// <summary>
    /// <para>Returns the absolute path to the Company addins folder.</para>
    /// </summary>
    [DataMember]
    public string AddinsFolder
    {
        get
        {
            return CreatePath(Path.Combine(CompanyHomeFolder, @"addins"));
        }
    }

    #endregion

    #region Private Members

    /// <summary>
    /// <para>Checks if the specified path exists, and creates the path 
    /// if the system cannot find it.</para>
    /// </summary>
    /// <param name="path">The path to verify.</param>
    private static string CreatePath(string path)
    {
        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);
        return path;
    }

    #endregion
}
}

所有这些都是非常基本的东西。WCF服务是由我们使用通过.NET提供的工厂和类动态创建的,WCF服务非常适合服务中已经存在的所有代码。

因此,我决定在类(这是我们的服务)中添加以下一行:

代码语言:javascript
复制
    /// <summary>
    /// <para>Returns the PathHelper to construct the various paths for API Scripts.</para>
    /// </summary>
    /// <returns>An instance of the PathHelper.</returns>
    public Objects.Helpers.IPathHelper GetPathHelper()
    {
        return new Helpers.PathHelper();
    }

    #endregion

当我运行unittest时,除了检查PathHelper函数的测试之外,所有测试都正常工作,它们最终都得到了相同的错误消息/异常:

错误1 'TenForce.Execution.API.ImplementationTest/HelperTests/CheckApplicationFolderPath‘TestCase失败:执行System.ServiceModel.CommunicationException:远程端点不再识别此序列。这很可能是由于远程端点上的中止造成的。wsrm:标识符的值不是已知的序列标识符。可靠的会话失败了。 服务器堆栈跟踪:在System.ServiceModel.Channels.ReliableRequestSessionChannel.SyncRequest.WaitForReply(TimeSpan超时)在System.ServiceModel.Channels.RequestChannel.Request(Message消息,TimeSpan超时)在System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message消息,TimeSpan超时)在System.ServiceModel.Channels.ServiceChannel.Call(String操作,布尔单向操作,ProxyOperationRuntime操作,Object[] ins,Object[]超时,TimeSpan超时)在System.ServiceModel.Channels.ServiceChannel.Call(String操作,布尔单向操作,ProxyOperationRuntime操作,Object[] ins,( System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime操作)在System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage消息上) 异常重新引发: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData,( TenForce.Execution.API.Contracts.IAPI.GetPathHelper() at TenForce.Execution.API.ServiceClient.ServiceAPI.GetPathHelper() at c:\Users\arne.de.herdt\Documents\Trunk\Robinson\TenForce.Execution.API.ServiceClient\ServiceAPI.cs:line 163 at TenForce.Execution.API.ImplementationTest.HelperTests.CheckApplicationFolderPath() in C:\Users\arne.de.herdt\Documents\Trunk\Robinson\TenForce.Execution.API.ImplementationTest\HelperTests.cs:line 56 c:\Users\arne.de.herdt\Documents\Trunk\Robinson\TenForce.Execution.API.ServiceClient\ServiceAPI.cs 163

我不知道哪里出了问题,或者我错过了什么。代码正在为已经存在的代码工作,但是当我添加我的部分时,它变得混乱了,但是现有的函数仍然在工作。是我造成的麻烦。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-10 09:45:54

由于来自注释和awnser的反馈,解决方案是将类移动到第二个API实现,而不是通过WCF服务使其变得更容易实现。

类包含函数和只读属性,因此WCF服务不能序列化该类。最终的结果将是只有脚本才能使用它,而不是服务。

票数 0
EN

Stack Overflow用户

发布于 2011-03-10 09:24:46

这个错误在我看来很奇怪,可能与您动态生成服务的方式有关。

但是,该类是不可序列化的,该类上的属性是只读的(没有集合访问器)。要将属性标记为DataMember,属性需要有一个集合访问器,即使它被标记为私有。来自MSDN:

注已应用DataMemberAttribute属性的属性必须同时具有get字段和set字段;它们不能仅获取或仅设置。

DataMember文档

您可能希望在该类中序列化的唯一东西是m_ParameterBul变量,因此将其标记为DataMember并从只读属性中删除所有其他DataMember属性即可。

您应该注意,如果m_ParameterBul不依赖于服务器,则不需要在服务器端创建该类,因为所有内容都与客户端相关。在这种情况下,您应该直接在客户端上创建它。

希望能帮上忙!

代码语言:javascript
复制
/// <summary>
/// <para>This class provides a direct implementation of the IPathHelper for the API implementation
/// and manages all the paths inside the DataHomeFolder structure for the TenForce application.</para>
/// </summary>
[DataContract]
public class PathHelper : Objects.Helpers.IPathHelper
{
    #region Private Fields
    [DataMember]
    private readonly ParameterBUL _mParameterBul;
    private const Parameter.ParameterId DataHomeFolderId = Parameter.ParameterId.DataHomeFolder;
    private const Parameter.ParameterId CompanyNameId = Parameter.ParameterId.CompanyName;

    #endregion

    #region Constructor

    /// <summary>
    /// <para>Creates a new instance of the PathHelper class</para>
    /// </summary>
    public PathHelper()
    {
        _mParameterBul = new ParameterBUL();
    }

    #endregion

    #region IPathHelper Members

    /// <summary>
    /// <para>Returns the absolute path to the DataHomeFolder of the TenForce Application.</para>
    /// </summary>   
    public string ApplicationFolder
    {
        get
        {
            return CreatePath(_mParameterBul.GetParameterValue(DataHomeFolderId));
        }
    }

    /// <summary>
    /// <para>Returns the absolute path to the Company DataHomeFolder.</para>
    /// </summary>   
    public string CompanyHomeFolder
    {
        get
        {
            return CreatePath(Path.Combine(ApplicationFolder, _mParameterBul.GetParameterValue(CompanyNameId)));
        }
    }

    /// <summary>
    /// <para>Returns the absolute path to the Company custom folder.</para>
    /// </summary>
    public string CustomFolder
    {
        get
        {
            return CreatePath(Path.Combine(CompanyHomeFolder, @"custom"));
        }
    }

    /// <summary>
    /// <para>Returns the absolute path to the Company wiki folder.</para>
    /// </summary>
    public string WikiFolder
    {
        get
        {
            return CreatePath(Path.Combine(CompanyHomeFolder, @"wiki"));
        }
    }

    /// <summary>
    /// <para>Returns the absolute path to the Company addins folder.</para>
    /// </summary>    
    public string AddinsFolder
    {
        get
        {
            return CreatePath(Path.Combine(CompanyHomeFolder, @"addins"));
        }
    }

    #endregion

    #region Private Members

    /// <summary>
    /// <para>Checks if the specified path exists, and creates the path 
    /// if the system cannot find it.</para>
    /// </summary>
    /// <param name="path">The path to verify.</param>
    private static string CreatePath(string path)
    {
        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);
        return path;
    }

    #endregion
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5257161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档