- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
ClientBase 的 C# 定义是:
public abstract class ClientBase<TChannel> : ICommunicationObject,
IDisposable
where TChannel : class
这清楚地表明了 TChannel
类型上的 class
约束。据我所知,这意味着您不能在声明您自己的类时使用泛型的接口(interface)类型。因此,给定这样声明的服务:
public IMyService
...
public MyService : IMyService
...
这应该有效:
public MyServiceClient : ClientBase<MyService>
这应该不:
public MyServiceClient : ClientBase<IMyService>
但显然我不理解,因为该示例显示了以下声明:
public partial class SampleServiceClient :
System.ServiceModel.ClientBase<ISampleService>, ISampleService
更重要的是,我正在尝试抽象身份验证,并使用实用程序方法正确关闭客户端:
public TResult WithClient<TInterface, T, TResult>(T service,
Func<TInterface, TResult> callback)
where T : ClientBase<TInterface>, TInterface
{
service.ClientCredentials.UserName.UserName = userName;
service.ClientCredentials.UserName.Password = password;
try
{
var result = callback(service);
service.Close();
return result;
}
catch (Exception unknown)
{
service.Abort();
throw unknown;
}
}
但这给了我编译器错误:
The type 'TInterface' must be a reference type in order to use it as parameter 'TChannel' in the generic type or method 'ClientBase<TChannel>'
有人可以消除这里的困惑吗?我做错了什么?
----更新----
根据@InBetween,解决方案是将 where TInterface : class
约束添加到我的实用程序方法中:
public TResult WithClient<TInterface, T, TResult>(T service,
Func<TInterface, TResult> callback)
where TInterface : class
where T : ClientBase<TInterface>, TInterface
{
service.ClientCredentials.UserName.UserName = userName;
service.ClientCredentials.UserName.Password = password;
try
{
var result = callback(service);
service.Close();
return result;
}
catch (Exception unknown)
{
service.Abort();
throw unknown;
}
}
最佳答案
class
constraint 将泛型类型约束为引用类型。根据定义,接口(interface)是引用类型。你不能做的是使用值类型作为通用类型:ClientBase<int>
将是一个编译时错误。
至于第二个错误,你没有约束TInterface
然后在 ClientBase<TInterface>
中使用它.因为ClientBase
将其泛型类型约束为引用类型( class
),您需要约束 TInterface
相应地。
关于具有接口(interface)混淆的 C# 通用 ClientBase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40949583/
我尝试在我的 MonoTouch 应用程序中调用 wcf 网络服务。在模拟器上一切正常。在设备上我收到错误:EXC_CRASH (SIGABRT) 我使用以下工具生成我的代理类:SlSvcUtil.e
我有以下代码: public partial class MyServiceClient : System.ServiceModel.ClientBase 派生,请使用此选项 m_MyClient.S
我正在开发使用网络服务的 xamarin 应用程序。 Xamarin 是否支持 System.ServiceModel.ClientBase 中的 X509Certificate? 在使用 .Net
如果 Dispose() 方法声明不可见/未声明,System.ServiceModel.ClientBase 抽象类如何实现 IDisposable 接口(interface)? 如果我尝试做同样的
我有一个应用程序,它使用由服务引用生成的 ClientBase 对象来调用第三方 WCF SOAP 服务。每隔一段时间,服务调用就会返回一个错误异常,而不是带有“处理请求时发生错误”的异常消息,这是完
我正在使用一个扩展 ClientBase<> 的类: interface IService {} class MyServiceClient : ClientBase {} 我遇到的问题是 FxCop
ClientBase 的 C# 定义是: public abstract class ClientBase : ICommunicationObject, IDisposable where
System.ServiceModel.Clientbase.Open() 有什么作用?我从未使用过它,只是在一些代码中遇到过它。它能抛出异常吗?如果不调用 Close() 是否有问题? 最佳答案 如
我有一个 WCF 客户端/服务器,客户端代理正在使用 ClientBase 。当访问客户端代理时,对任何服务的第一次调用大约需要 8 秒。后续调用(在 15 秒超时内)需要约 0.5 秒。我找到了以下
我正在研究 ClientBase 中采用的使用模式和 ChannelFactory有一点我无法理解。 在大多数例子中,我们都有这种用法 public class MyClient : ClientBa
我正在将 .NET WCF 应用程序移至 Mono 以在 iOS 中使用。因为iOS中的Mono不支持动态代码生成,所以我需要重写ClientBase.CreateChannel ,如本文所述: Mo
我正在开发一个包含 WCF 服务和使用该服务的客户端的解决方案。有时我正在调试服务,有时是客户端,有时两者都是。 在调试期间,我得到一个带有附加信息的 TimeoutException 附加信息:请求
我们目前正在使用 Web 服务 (IBM Message Broker)。由于该服务仍在开发中,在许多情况下它会返回无效的 XML(是的,我保证会修复这个问题)。 从 .NET 调用此服务时出现问题,
我有一个第三方 API 的服务引用。我使用自定义 ChannelFactory 类来构建此 API 的 channel (类型为 System.ServiceModel.ClientBase 的 [W
前一段时间我看到一篇有趣的文章,解释了将 HttpClient在 using block 中将在代码执行但不关闭 TCP 套接字时处理该对象,并且 TCP 状态最终将进入 TIME_WAIT 并在该状
在早期版本的 C#.Net 中,处理 WCF 代理(服务引用生成的客户端)时存在问题 - 这在 .Net 4.5+ 中是否仍然存在? 可以在此处找到此问题的完整详细信息... https://codi
我是一名优秀的程序员,十分优秀!