我正在寻找一种方法来允许C#对象中的属性只设置一次.编写代码很容易,但我宁愿使用标准机制(如果存在).
public OneShot<int> SetOnceProperty { get; set; }
我想要发生的是,如果属性尚未设置,则可以设置属性,但如果之前已设置,则抛出异常.它应该像Nullable值一样运行,我可以检查它是否已设置.
当我在我的解决方案中新建一个WCF服务时,我可以执行以下操作,有一个带参数的构造函数来传入吗?如果是,运行时如何,何时以及在何处填写我所需的IBusinessLogic对象?
[ServiceContract]
public interface IServiceContract
{
[OperationContract]
...
}
public class MyService : IServiceContract
{
IBusinessLogic _businessLogic;
public ServiceLayer(IBusinessLogic businessLogic)
{
_businessLogic = businessLogic;
}
...
}
Run Code Online (Sandbox Code Playgroud) 在我的WCF服务中,我有一个自定义消息检查器,用于将传入消息验证为XML Schema的原始XML.消息检查器具有一些依赖性(例如记录器和XML架构集合).我的问题是,我可以使用依赖注入框架(我目前正在使用Ninject)来实例化这些自定义行为并自动注入依赖项吗?
我做了一个简单的例子来展示这个概念:
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using Ninject.Extensions.Logging;
public class LogMessageInspector : IDispatchMessageInspector
{
private readonly ILogger log;
public LogMessageInspector(ILogger log)
{
this.log = log;
}
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
LogMessage(ref request);
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
LogMessage(ref reply);
}
private void LogMessage(ref Message message)
{
//... copy the message and log using this.log ...
}
}
public class LogMessageBehavior : IEndpointBehavior
{
private readonly …Run Code Online (Sandbox Code Playgroud) 我知道有关于WCF回调和事件的教程,但是我无法让它们实际工作,或者它们太复杂了.我是一个真正的初学者,所以我很想听听是否有人知道任何可靠的初学者教程,这些教程的目标是我现在想要具体解决的问题.如果我使用错误的术语(请纠正我),请原谅我,就像我说我是初学者一样.
问题:

它可能看起来比实际上更复杂.基本上我想要完成的是:
我可以做什么:
我可以设置连接,但我的服务仅限于独立功能.客户端可以进行"查询",但仅限于远程函数调用(例如"add",其中所有参数都随函数传递,处理在内部完成).
我想弄清楚的是:
TLDR:
主持人:服务< - >客户端.有没有办法将数据(只是一个int)推送到客户端而无需客户端调用任何函数(没有轮询或查询)?有没有办法让WCF服务访问变量存储在主机应用程序的实例中而不使用静态成员?这可以通过简单的方式完成吗?
谢谢你的帮助和时间,我知道我写了一本书. 如果有人知道任何好的教程,请指出他们. 但请不要 - 请不要指向Add(int x,int y)示例,其中客户端只在主机上调用add并返回结果 - 我已经完成了几次这样做并且它没有帮助我掌握WCF的真正功能.我真的不想在这一点上做任何严肃的事情,我真的想保持简单,所以我可以学习WCF可以做什么,而且我没有发现文档非常有用.再次感谢大家.
我正在做以下事情
//Define the service host
this._smeediPluginServiceHost = new ServiceHost(typeof(SmeediServiceHost), smeediServiceUri);
this._smeediPluginServiceHost.AddServiceEndpoint(typeof(ISmeediServiceHost), GetBinding(), smeediServiceUri);
SetupAndStartWebService(_smeediPluginServiceHost);
private void SetupAndStartWebService(ServiceHost serviceHost, ServiceDiscoveryBehavior serviceDiscoveryBehavior = null)
{
//Define service behaviours
ServiceMetadataBehavior serviceMetadataBehavior = new ServiceMetadataBehavior();
serviceMetadataBehavior.HttpGetEnabled = true;
//Add the behaviours to the service
serviceHost.Description.Behaviors.Add(serviceMetadataBehavior);
if (serviceDiscoveryBehavior != null)
serviceHost.Description.Behaviors.Add(serviceDiscoveryBehavior);
serviceHost.Open();
}
Run Code Online (Sandbox Code Playgroud)
我需要将一个参数传递给服务,我无法弄清楚如何.我看过如何将值传递给我的wcf服务上的构造函数?但无法理解我的想法.谢谢
我在尝试在WCF应用程序中初始化我的IOC容器时遇到问题.该应用程序是基于非HTTP的使用WAS.
当我启动应用程序时,我收到此错误消息...
Kernel was null, did you forgot to call DefaultServiceHostFactory.RegisterContainer
我已经看到了这个解决方案的其他答案,它说要做以下事情......
Factory="Castle.Facilities.WcfIntegration.DefaultServiceHostFactory, Castle.Facilities.WcfIntegration"
我已经这样做但我的AppInitialize方法没有被调用,我仍然得到上面的错误.
谢谢.
在我的托管应用程序中,我当前的 WCF 服务运行为:
SomeService service = new SomeService(container) //IUnityContainer
ServiceHost serviceHost = new ServiceHost(service, serviceAddress);
Run Code Online (Sandbox Code Playgroud)
有什么问题吗?SomeService 定义为:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single
Run Code Online (Sandbox Code Playgroud)
这不再好了,我需要将其设为InstanceContextMode.PerCall。
当尝试 .Open() 时,如果将 InstanceContextMode 更改为“PerCall” - 它将抛出:
System.InvalidOperationException: In order to use one of the ServiceHost constructors that takes a service instance, the InstanceContextMode of the service must be set to InstanceContextMode.Single. This can be configured via the ServiceBehaviorAttribute. Otherwise, please consider using the ServiceHost constructors that take a Type argument
Run Code Online (Sandbox Code Playgroud)
这是我的问题的解决方案吗?如何将值传递给 wcf 服务上的构造函数?
我主要关心的是:
我在这个托管应用程序中运行不同类型的服务,似乎只有当我运行一种类型的服务时,这个解决方案才是好的。
我想使用Ninject.Wcf扩展来创建参数化服务主机实例.
例如,我有一个MyWCFHandler只有以下构造函数的类:
public MyWCFHandler(UserManager manager)
{
_manager = manager;
}
Run Code Online (Sandbox Code Playgroud)
但是当我写作时,我var myServiceHost = new ServiceHost(typeof(MyWCFHandler));无法将依赖项对象传递给构造函数.
我不想搞乱如何在我的wcf服务上将值传递给构造函数中提供的自定义ServiceHost ?
我决定采用Ninject的方式,但不能完全理解如何在我的情况下采取行动.
以下是我对Ninject中的WCF扩展的理解:
创建一个继承Ninject模块的类,并编写如下内容:
internal class ServiceModule : NinjectModule
{
public override void Load()
{
Bind<IUserManager>().To<UserManager>().WithConstructorParameters(myUserManagerIwantToUseInWCFHandler);
}
}
将使用新ServiceModule()初始化的内核添加到KernelContainer.
像这样使用NinjectServiceHost:
var service = KernelContainer.Kernel.Get<IMyWCFHandler>();
_host = new NinjectServiceHost( service );
在那里我应该准备好我的主人打开.
问题是:
我应该如何将构造函数参数传递给NinjectModule?当我准备将参数绑定到它时,我应该创建NinjectModule的实例吗?如何将它们传递给Get方法?
遗憾的是,没有一个示例可以简单地显示参数化的ServiceHost启动.我甚至不关心它是否是我使用的Ninject.无论哪种解决方案都有一个很好的例子 - 我很好,因为我只是决定使用什么IoC容器.
wcf ×7
c# ×5
ninject ×2
servicehost ×2
.net ×1
constructor ×1
parameters ×1
service ×1
singleton ×1
was ×1