从WCF中的通用合约继承

2023-11-21

更多 WCF 困境...:)

我的所有工作流程都实现相同的 3 种方法。经过大量的复制和粘贴,我决定让它们继承同一个接口:

[ServiceContract(Namespace = "http://schema.company.com/messages/")]
public interface IBasicContract<TRequest, TResponse>
  where TRequest : class
  where TResponse : class
{
  [OperationContract(Name = "GetReport",
    Action = "http://schema.company.com/messages/GetReport",
    ReplyAction = "http://schema.company.com/messages/GetReportResponse")]
  TResponse GetReport(TRequest inquiry);

  [OperationContract(Name = "GetRawReport",
    Action = "http://schema.company.com/messages/GetRawReport",
    ReplyAction = "http://schema.company.com/messages/GetRawReportResponse")]
  string GetRawReport(string guid);

  [OperationContract(Name = "GetArchiveReport",
    Action = "http://schema.company.com/messages/GetArchiveReport",
    ReplyAction = "http://schema.company.com/messages/GetArchiveReportResponse")]
  TResponse GetArchiveReport(string guid);
}

我还决定创建服务客户端的通用实现:

public class BasicSvcClient<TRequest, TResponse> : ClientBase<IBasicContract<TRequest, TResponse>>, IBasicContract<TRequest, TResponse>
  where TRequest : class
  where TResponse : class
{
  public BasicSvcClient()
  {
  }

  public BasicSvcClient(string endpointConfigurationName) :
    base(endpointConfigurationName)
  {
  }

  public BasicSvcClient(string endpointConfigurationName, string remoteAddress) :
    base(endpointConfigurationName, remoteAddress)
  {
  }

  public BasicSvcClient(string endpointConfigurationName, EndpointAddress remoteAddress) :
    base(endpointConfigurationName, remoteAddress)
  {
  }

  public BasicSvcClient(Binding binding, EndpointAddress remoteAddress) :
    base(binding, remoteAddress)
  {
  }

  public TResponse GetReport(TRequest inquiry)
  {
    return Channel.GetReport(inquiry);
  }

  public string GetRawReport(string guid)
  {
    return Channel.GetRawReport(guid);
  }

  public TResponse GetArchiveReport(string guid)
  {
    return Channel.GetArchiveReport(guid);
  }
}

问题是当我尝试使用这个时:

using (var client = new BasicSvcClient<TRequest, TResponse>())
{
  var response = client.GetReport(inquiry);

  context.Response.ContentType = "text/xml";
  context.Response.Write(response.AsXML());
}

我总是收到一条错误消息,说它找不到合约 IBasicContract 的配置,其中 .NET 使用的奇怪语法是:

找不到默认端点 引用合同的元素 'BasicWorkflow.IBasicContract`2...

我尝试这样做:

using (var client = new BasicSvcClient<TRequest, TResponse>("myConfig"))

这没有帮助——它仍在寻找特定的合同。

我知道 ServiceContract 属性有一个 ConfigurationName 参数,但我无法在编译时使用它,因为我有many我从同一程序调用的工作流程(因此有许多配置条目)。有没有办法在运行时设置 ConfigurationName?我认为这就是 ClientBase 构造函数应该做的事情,但显然不是。

[编辑] 这是 .config 文件中的端点,我认为在这种情况下它不是很有帮助:

<endpoint address="https://localhost/services/Contract.svc"
    binding="basicHttpBinding"
    bindingConfiguration="httpsDataEndpoint"
    contract="IContract" name="IContractSvc" />

[Edit2] 好的...我找到了一种有效的方法,尽管我仍然不完全满意:

using (var wf = new BasicSvcClient<TRequest, TResponse>(
  new BasicHttpBinding("httpsDataEndpoint"),
  new EndpointAddress("https://localhost/services/Contract.svc")))

我现在遇到的唯一问题是我更愿意从 .config 文件中检索端点地址(使用实际的合约名称,如 IContract)。有人可以帮助我完成那部分吗?

[Edit3] 终于找到了完整的解决方案:) Reflector万岁!

var cf = (ClientSection) ConfigurationManager.GetSection("system.serviceModel/client");
foreach (ChannelEndpointElement endpoint in cf.Endpoints)
{
  if (endpoint.Name != "ContractSvc")
    continue;

  using (var wf = new BasicSvcClient<TRequest, TResponse>(
    new BasicHttpBinding("httpsDataEndpoint"),
    new EndpointAddress(endpoint.Address.ToString())))
  {
      //... call wf.GetReport()
  }
  break;
}

“.NET 使用的奇怪语法”实际上是运行时绑定到特定类型的泛型类型的类型名称。 Typename`n[[Type],...] 其中 n 表示泛型类型中包含的类型参数的数量。

那么您的端点配置是什么样的?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从WCF中的通用合约继承 的相关文章

随机推荐

  • 当测试和源在一起时,Sonarqube 测试报告“报告指的是未配置为测试文件的文件”

    我正在使用 TypeScript 和 Jest 并在源文件旁边进行测试 例如 someDir 一些代码 ts 一些代码规范 ts 当我尝试导入 text report xml 看起来很好并且与格式匹配 时 我收到一条错误消息 第 X 行报告
  • 有效地将序列列表拆分为两个列表[重复]

    这个问题在这里已经有答案了 可能的重复 Python 中的转置 解压缩函数 我有一个序列列表 每个序列有两个项目 我想把它变成两个列表 catalog abc 123 foo 456 bar 789 test 1337 现在我只是这样做 n
  • 使用 RxJs Observable 实现延迟队列

    Imagine we have a queue of booleans we don t need a complex datastructure since we wanna store only the fact of the orde
  • R - 逐行读取 STDIN

    我想将大数据表逐行流式传输到 R 中 如果当前行有特定条件 假设第一列 gt 15 则将该行添加到内存中的数据帧中 我写了以下代码 count lt 1 Mydata lt NULL fin lt FALSE while fin if co
  • .NET 不同的应用程序设置用于开发和发布

    我正在使用 VS2010 C NET 3 5 和应用程序设置 Settings settings 文件 我想要做的是为我的开发和生产环境设置不同的设置 而不必在代码中使用检查调试模式的条件语句 解决这个问题的常见方法是什么 或者您可以创建单
  • AttributeError:“RegexURLResolver”对象没有属性“_urlconf_module”

    我在哨兵异常中不断收到以下错误 AttributeError RegexURLResolver object has no attribute urlconf module 并且跟踪仅指向 django 代码库中的代码 而不指向我的应用程序
  • MSysQueries 中的数据意味着什么?

    我一直在使用 VBA 检查 Access 2000 数据库中的所有查询 表单和模块 但它可能非常乏味且缓慢 最近 我决定仔细研究 Access 中的系统表 特别是 MSysQueries 和 MSysObjects 我可以使用这些表更快地检
  • std::vector 可以像数组一样对待吗

    Can a std vector
  • Chrome 上“overflow: auto”的奇怪行为

    我正在开发一个带有博客部分的网站 我需要这个部分有固定的高度 为了能够看到博客中的所有帖子 我添加了一个溢出 自动所以它会在需要时显示滚动条 div div div class post This is a long post div di
  • 如何在 django 中更新 m2m 字段

    I have class MyUser Model today ref viewed ips ManyToManyField UniqAddress related name today viewed users verbose name
  • 在.net core 3.1应用程序中使用AddEnvironmentVariables

    我已经生成了一个新的网络项目 似乎在 net core 3 1 中 appSettings jsons 已生成并且工作正常 问题是它们是由运行时而不是我加载和控制的 所以我无法调用AddEnvironmentVariables 哪里才是正确
  • localstorage 和 setInterval() 具有多个选项卡

    我们有一些数据存储在localstorage我们正在使用window setInterval 每分钟定期更新一次 在此期间我们不断地读取和写入数据 是否有可能出现并发问题 因为我们正在使用setInterval 因为多个选项卡可以修改其中的
  • 如何使用.net更改Windows2k8的时区设置[重复]

    这个问题在这里已经有答案了 我已经尝试用 C 代码更改系统的时区几天了 但我所做的一切都没有多大意义或根本不起作用 我开始尝试使用我在其他问题中看到的 SetTimeZoneInformation 的 PInvoke 语法 DllImpor
  • 使用 Perl,如何比较 YYYY-MM-DD 形式的日期?

    我有一个数组nYYYY MM DD 格式的字符串 例如 2010 10 31 如何将日期与该数组中的字符串进行比较 比如删除30多天前的字符串 伟大的事情是YYYY MM DD 格式化日期是您可以使用简单的字符串比较来比较它们 在 Perl
  • @parcel/core:找不到 .glb 文件的变压器

    当我将 Parcel js 部署到 Vercel 时 出现此错误 parcel core No transformers found for static actions glb 以下是 Vercel 的完整部署日志 Detected pa
  • IE 中使用 ES6 箭头函数的语法错误

    我有这段 JavaScript 代码 price price replace x gt x replace g 这在 Firefox 和 Chrome 中工作得很好 但是 IE 给了我一个语法错误 指向 gt 在我的代码中 有没有办法在 I
  • 为 anaconda python 安装 Ipopt

    有人在 Anaconda python 上安装过 Ipopt 吗 我下载的是3 6 1版本 另外 我下载了请求英特尔 Fortran 库如自述文件中所述 安装应该直接使用configure make and make install以及与其
  • 如何强制转换重载的自由函数来解决重载冲突?

    假设你有 2 个免费函数 void do something dog d void do something cat c 不说你想将这些函数传递给模板化函数 template
  • MVC4 RC 脚本捆绑非常慢

    我今天将一个大型项目升级到 MVC4 RC 以尝试捆绑和缩小 之前我在部分视图中的脚本 src 标记中有 8 个静态 js 文件 示例页面的运行时间始终低于 0 1 秒 我添加了一个像这样的包 bundles Add New ScriptB
  • 从WCF中的通用合约继承

    更多 WCF 困境 我的所有工作流程都实现相同的 3 种方法 经过大量的复制和粘贴 我决定让它们继承同一个接口 ServiceContract Namespace http schema company com messages publi