{"id":22290645,"url":"https://github.com/ramondeklein/wcfstructuremap","last_synced_at":"2025-03-25T21:33:15.688Z","repository":{"id":89300659,"uuid":"160803756","full_name":"ramondeklein/wcfstructuremap","owner":"ramondeklein","description":"Use StructureMap with WCF","archived":false,"fork":false,"pushed_at":"2018-12-07T16:32:58.000Z","size":17,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-30T18:48:57.972Z","etag":null,"topics":["structuremap","wcf"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ramondeklein.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-12-07T09:44:00.000Z","updated_at":"2020-03-04T14:02:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"11977da2-9888-4cf6-802f-3c026741b6a0","html_url":"https://github.com/ramondeklein/wcfstructuremap","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ramondeklein%2Fwcfstructuremap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ramondeklein%2Fwcfstructuremap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ramondeklein%2Fwcfstructuremap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ramondeklein%2Fwcfstructuremap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ramondeklein","download_url":"https://codeload.github.com/ramondeklein/wcfstructuremap/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245548570,"owners_count":20633611,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["structuremap","wcf"],"created_at":"2024-12-03T17:13:38.310Z","updated_at":"2025-03-25T21:33:15.678Z","avatar_url":"https://github.com/ramondeklein.png","language":"C#","readme":"# WCF with StructureMap\nThe articles discussing WCF and StructureMap together are fairly old. I have\ncreated a minimal application that uses WCF and StructureMap together.\n\n# Explanation\nWCF creates instances via the [`IInstanceProvider`](https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.dispatcher.iinstanceprovider)\ninterface, so we need to create a custom instance provider that creates the\ninstances via StructureMap. This instance provider uses the container to\nresolve the requested types:\n```\npublic class StructureMapInstanceProvider : IInstanceProvider\n{\n    private readonly IContainer _container;\n    private readonly Type _type;\n\n    public StructureMapInstanceProvider(IContainer container, Type type)\n    {\n        _container = container;\n        _type = type;\n    }\n\n    public object GetInstance(InstanceContext instanceContext)\n    {\n        return _container.GetInstance(_type);\n    }\n\n    public object GetInstance(InstanceContext instanceContext, Message message)\n    {\n        return GetInstance(instanceContext);\n    }\n\n    public void ReleaseInstance(InstanceContext instanceContext, object instance)\n    {\n        _container.Release(instance);\n    }\n}\n```\nEach endpoint can have its own instance provider by setting the\n[`InstanceProvider`](https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.dispatcher.dispatchruntime.instanceprovider)\nof the [`DispatchRuntime`](https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.dispatcher.dispatchruntime)\nclass. This is typically done from inside a service behavior\n([`IServiceBehavior`](https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.description.iservicebehavior))\nin the\n[`ApplyDispatchBehavior`](https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.description.iservicebehavior.applydispatchbehavior)).\n```\npublic class StructureMapServiceBehavior : IServiceBehavior\n{\n    private readonly IContainer _container;\n\n    public StructureMapServiceBehavior(IContainer container)\n    {\n        _container = container;\n    }\n\n    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)\n    {\n    }\n\n    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection\u003cServiceEndpoint\u003e endpoints, BindingParameterCollection bindingParameters)\n    {\n    }\n\n    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)\n    {\n        var instanceProvider = new StructureMapInstanceProvider(_container, serviceDescription.ServiceType);\n        foreach (var channelDispatcher in serviceHostBase.ChannelDispatchers.OfType\u003cChannelDispatcher\u003e())\n        {\n            foreach (var ed in channelDispatcher.Endpoints)\n                ed.DispatchRuntime.InstanceProvider = instanceProvider;\n        }\n    }\n}\n```\nThe behavior needs to be added to the\n[`ServiceHost`](https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.servicehost)\nso it is applied to all services within that host:\n```\nserviceHost.Description.Behaviors.Add(new StructureMapServiceBehavior(container));\n```\nThe services will now be created via StructureMap and you can use dependency\ninjection in your services. This solution doesn't use any static variables or\nfactory patterns (as seen in some other examples that I have seen).\n\n# Nested containers\nYou probably want nested containers when the\n[`InstanceContextMode`](https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.instancecontextmode)\nof your service is set to `PerCall` or `PerSession`. You need to create these\nnested containers in the\n[`IInstanceProvider.GetInstance`](https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.dispatcher.iinstanceprovider.getinstance)\ncall and dispose the nested container in the\n[`IInstanceProvider.ReleaseInstance`](https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.dispatcher.iinstanceprovider.releaseinstance).\nTo make sure you can correlate the `GetInstance` and `ReleaseInstance` additional\ninformation can be added to the instance context (via the\n[`Extensions`](https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.instancecontext.extensions)\nproperty. You can find the required changes in commit \n[6d7e0de8d7501469bbfe9944e32e111b3e516e63](https://github.com/ramondeklein/wcfstructuremap/commit/6d7e0de8d7501469bbfe9944e32e111b3e516e63).\n\n# Singleton services (caveat)\nSingletons are already created in the constructor of the\n[`ServiceHost`](https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.servicehost),\nso you cannot use a behavior to alter the creation. Singletons can only be\ncreated in WCF if they have a default constructor (and IoC services typically\ndon't have these). If you want to use a service host with StructureMap and\nsingletons, then you can pass the singleton directly to the container:\n```\nusing (var container = Register())\n{\n    var singleton = container.Resolve\u003cHelloWcfServer\u003e();\n    using (var host = new ServiceHost(singleton, Settings.BaseUri))\n    {\n        // ...\n    }\n}\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Framondeklein%2Fwcfstructuremap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Framondeklein%2Fwcfstructuremap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Framondeklein%2Fwcfstructuremap/lists"}