Created
January 24, 2017 10:55
-
-
Save codingarchitect/f5c929b9092c264e9f547c5486981918 to your computer and use it in GitHub Desktop.
WindsorWcfService.linq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Query Kind="Program"> | |
<Reference><RuntimeDirectory>\Microsoft.CSharp.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Runtime.InteropServices.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Runtime.InteropServices.WindowsRuntime.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Runtime.Serialization.dll</Reference> | |
<Reference><RuntimeDirectory>\System.ServiceModel.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Web.dll</Reference> | |
<NuGetReference>Castle.WcfIntegrationFacility</NuGetReference> | |
<NuGetReference>CodingArchitect.Utilities</NuGetReference> | |
<NuGetReference>Microsoft.Web.Administration</NuGetReference> | |
<NuGetReference>NUnitLite</NuGetReference> | |
<Namespace>Microsoft.CSharp</Namespace> | |
<Namespace>Microsoft.CSharp.RuntimeBinder</Namespace> | |
<Namespace>Microsoft.Web.Administration</Namespace> | |
<Namespace>NUnit.Framework</Namespace> | |
<Namespace>NUnitLite</Namespace> | |
<Namespace>System.CodeDom.Compiler</Namespace> | |
<Namespace>System.Runtime.InteropServices</Namespace> | |
</Query> | |
void Main() | |
{ | |
var queryDirectory = Path.GetDirectoryName(Util.CurrentQueryPath); | |
Environment.CurrentDirectory = queryDirectory; | |
var companyName = "CodingArchitect"; | |
var serviceWebsiteName = "WindsorWcfService"; | |
var port = "9999"; | |
var webServiceTypeName = "GreeterService"; | |
var appPhysicalPath = Path.Combine(queryDirectory, serviceWebsiteName); | |
var binDirectoryPath = Path.Combine(appPhysicalPath, "bin"); | |
CodingArchitect.Utilities.Linqpad.Util.SetupWebService(companyName, serviceWebsiteName, port, webServiceTypeName, queryDirectory, Util.CurrentQueryPath); | |
CodingArchitect.Utilities.Linqpad.Compiler.CopyReferencesToAppDirectory(queryDirectory, Util.CurrentQueryPath, binDirectoryPath); | |
CreateConfigAndServiceFiles(appPhysicalPath, webServiceTypeName, companyName, serviceWebsiteName); | |
} | |
private static void CreateConfigAndServiceFiles(string appPhysicalPath, string webServiceTypeName, string companyName, string serviceWebsiteName) | |
{ | |
CreateIoCConfig(appPhysicalPath); | |
CreateGlobalAsax(appPhysicalPath); | |
CreateServiceFile(appPhysicalPath, webServiceTypeName, companyName, serviceWebsiteName); | |
} | |
private static void CreateIoCConfig(string appPhysicalPath) | |
{ | |
var iocConfigurationFileDirectory = Path.Combine(appPhysicalPath, "bin", "IOC"); | |
if (!Directory.Exists(iocConfigurationFileDirectory)) Directory.CreateDirectory(iocConfigurationFileDirectory); | |
var iocConfigurationFilePath = Path.Combine(iocConfigurationFileDirectory, "InstanceConfiguration.xml"); | |
CreateIoCConfigurationFile(iocConfigurationFilePath); | |
} | |
private static void CreateGlobalAsax(string appPhysicalPath) | |
{ | |
var globalAsaxPath = Path.Combine(appPhysicalPath, "Global.asax"); | |
File.WriteAllText(globalAsaxPath, "<%@ Application Inherits=\"CodingArchitect.Spikes.WindsorWcfService.Global, CodingArchitect.Spikes.WindsorWcfService\" Language=\"C#\" %>"); | |
} | |
private static void CreateServiceFile(string appPhysicalPath, string webServiceTypeName, string companyName, string serviceWebsiteName) | |
{ | |
var webServiceFilePath = Path.Combine(appPhysicalPath, webServiceTypeName + ".svc"); | |
//Factory="Castle.Facilities.WcfIntegration.DefaultServiceHostFactory, Castle.Facilities.WcfIntegration" | |
var namespaceQualifiedWebServiceTypeName = string.Format("{0}.Spikes.{1}.I{2}, {0}.Spikes.{1}", companyName, serviceWebsiteName, webServiceTypeName); | |
var serviceFactoryTypeName = "Factory=\"Castle.Facilities.WcfIntegration.DefaultServiceHostFactory, Castle.Facilities.WcfIntegration\""; | |
File.WriteAllText(webServiceFilePath, "<%@ ServiceHost Service=\"" + namespaceQualifiedWebServiceTypeName + "\" " + serviceFactoryTypeName + " %>"); | |
} | |
private static void CreateIoCConfigurationFile(string iocConfigurationFilePath) | |
{ | |
new XDocument( | |
new XElement("configuration", | |
new XElement("components", | |
new XElement("component", | |
new XAttribute("id", "GreeterService"), | |
new XAttribute("service", "CodingArchitect.Spikes.WindsorWcfService.IGreeterService, CodingArchitect.Spikes.WindsorWcfService"), | |
new XAttribute("type", "CodingArchitect.Spikes.WindsorWcfService.GreeterService, CodingArchitect.Spikes.WindsorWcfService"), | |
new XAttribute("lifestyle", "transient") | |
), | |
new XElement("component", | |
new XAttribute("id", "Greeter"), | |
new XAttribute("service", "CodingArchitect.Spikes.WindsorWcfService.IGreeter, CodingArchitect.Spikes.WindsorWcfService"), | |
new XAttribute("type", "CodingArchitect.Spikes.WindsorWcfService.Greeter, CodingArchitect.Spikes.WindsorWcfService"), | |
new XAttribute("lifestyle", "transient") | |
) | |
) | |
) | |
).Save(iocConfigurationFilePath); | |
} | |
// Define other methods and classes here | |
} | |
#region Service Classes | |
namespace CodingArchitect.Spikes.WindsorWcfService | |
{ | |
using System.ServiceModel; | |
using System.Web; | |
using Castle.MicroKernel.Registration; | |
using Castle.MicroKernel.Releasers; | |
using Castle.MicroKernel.Resolvers.SpecializedResolvers; | |
using Castle.Windsor; | |
using Castle.Windsor.Configuration.Interpreters; | |
using Castle.Facilities.WcfIntegration; | |
public static class InstanceFactory | |
{ | |
private static IWindsorContainer _container; | |
public static void InitializeContainer() | |
{ | |
StringBuilder basePath = new StringBuilder(AppDomain.CurrentDomain.BaseDirectory); | |
if (basePath.ToString().EndsWith("\\") == false) | |
basePath.Append("\\"); | |
if (!File.Exists(Path.Combine(basePath.ToString(), @"IOC\InstanceConfiguration.xml"))) | |
{ | |
if (basePath.ToString().Contains(@"\bin") == false) | |
basePath.Append(@"bin\"); | |
} | |
basePath.Append(@"IOC\InstanceConfiguration.xml"); | |
_container = new WindsorContainer(new XmlInterpreter(basePath.ToString())); | |
_container.AddFacility<WcfFacility>(f => f.CloseTimeout = TimeSpan.Zero); | |
var kernel = _container.Kernel; | |
kernel.ReleasePolicy = new NoTrackingReleasePolicy(); | |
kernel.Resolver.AddSubResolver(new CollectionResolver(kernel)); | |
//DefaultServiceHostFactory.RegisterContainer(_container); | |
} | |
public static void Register<TComponent, TService>() | |
where TComponent : class | |
where TService : TComponent | |
{ | |
_container.Register(Component.For<TComponent>() | |
.ImplementedBy<TService>() | |
.LifestyleTransient()); | |
} | |
public static T Resolve<T>() | |
where T : class | |
{ | |
return _container.Resolve<T>(); | |
} | |
public static void Release<T>(T service) | |
where T : class | |
{ | |
_container.Release(service); | |
} | |
} | |
public class Global : HttpApplication | |
{ | |
protected void Application_Start(object sender, EventArgs e) | |
{ | |
InstanceFactory.InitializeContainer(); | |
} | |
} | |
public class GreeterService : IGreeterService | |
{ | |
private readonly IGreeter greeter; | |
public GreeterService(IGreeter greeter) | |
{ | |
this.greeter = greeter; | |
} | |
public string Greet() | |
{ | |
return greeter.Greet(); | |
} | |
} | |
[ServiceContract] | |
public interface IGreeterService | |
{ | |
[OperationContract] | |
string Greet(); | |
} | |
public interface IGreeter | |
{ | |
string Greet(); | |
} | |
public class Greeter : IGreeter | |
{ | |
public string Greet() | |
{ | |
return "Hello World from Web Service"; | |
} | |
} | |
} | |
#endregion | |
class EOF { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment