Alternate for ServiceHost in .Net 5

4.8k Views Asked by At

Currently i am migrating my class library project from .net framework 4.6.1 to .Net 5. where i am using ServiceHost class to host wcf server. As The WCF Server functionality will not be supported in .Net. what is the work around for this.

public static ServiceHost CreateServiceHost(object singleObject, string baseAddress, Type serviceContract, string endPoint, bool isNetBinding)
    {
        if (InstEnvironment.Instance.LogVerbose)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("ServiceUtility.CreateServiceHost() entered...");
            sb.AppendFormat("\t\t\t  baseAddress: {0}", baseAddress);
            sb.AppendLine();
            sb.AppendFormat("\t\t\t  endPoint: {0}", endPoint);
            sb.AppendLine();
            sb.AppendFormat("\t\t\t  serviceContract: {0}", serviceContract.ToString());
            sb.AppendLine();
            sb.AppendFormat("\t\t\t  isNetBinding: {0}", isNetBinding.ToString());
            LogUtility.LogMessage(sb.ToString(), LogUtility.Log.MessageType.Verbose);
            sb.Length = 0;
        }

        Uri baseAddressUri = new Uri(baseAddress);
        ServiceHost sh = new ServiceHost(singleObject, new Uri[] { baseAddressUri });

        // Service Behavior
        ServiceDebugBehavior debugBehavior = sh.Description.Behaviors.Find<ServiceDebugBehavior>();
        if (debugBehavior == null)
        {
            debugBehavior = new ServiceDebugBehavior();
            sh.Description.Behaviors.Add(debugBehavior);
        }
        debugBehavior.IncludeExceptionDetailInFaults = true;

        ServiceMetadataBehavior metaDataBehavior = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();
        if (metaDataBehavior == null)
        {
            metaDataBehavior = new ServiceMetadataBehavior();
            sh.Description.Behaviors.Add(metaDataBehavior);
        }
1

There are 1 best solutions below

0
FireAlkazar On

CoreWCF repo is the server-side port of WCF for .NET Core and they have the sample server
https://github.com/CoreWCF/CoreWCF/tree/master/src/Samples/NetCoreServer

Some reasons behind it:

This isn't a straight port of WCF Server/ServiceHost. To do a straight port would have made cross platform support almost impossible. It would have meant more code in the repo, meaning more code which needs to be maintained and a higher chance of bugs on non-windows platforms.

https://github.com/CoreWCF/CoreWCF/issues/227#issuecomment-700295498