none
Hosting service in a conoel app RRS feed

  • Question

  • Dear all,
    I am trying using unity.wcf nuget component to create a servicehost which will be started from a simple console host for debuging.
    for that I have define my factory host class as below :

    public class WCFServiceFactory: UnityServiceHostFactory
        {
            protected override void ConfigureContainer(IUnityContainer container)
            {
                container
                    .RegisterType<IDataService, Method>()
                    .RegisterType<IUserRepository, UserRepository>()
                    .RegisterType<IUserData, UserData>();
    
            }
    
            protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
            {
                return base.CreateServiceHost(serviceType, baseAddresses);
            }
    
            
        }
    


    Then most of exemple found for instanciating a new ServiceHost from this class is from adding the Factory paramter inside the .svc service file markup as below :

    <%@ ServiceHost Language="C#" Debug="true" Service="MethodServices.Method" CodeBehind="Service.svc.cs" 
        Factory="MethodServices.WCFServiceFactory"%>

    By hosting my service under IIS, it works. What I am trying to do is how to call my ServiceHost from a console for debuging as below :

    _serviceHost = new ServiceHost(typeof(Method));
    
                // Open the ServiceHost to start listening for messages.
                _serviceHost.Open();

    If I run the code above I get an error because it says that I do not have a parameters less constructor, which is correct becuase my service constructor is injecting an interface using unity.wcf

    In order to instanceiate my service host I need to call the CreateServiceHost of my WCFServiceFactory declared as :

     protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
           

    As all my service end point adress are in my config file, I should be able to call it as  :

    CreateServiceHost(typeof(Method)) but problem is that it required a BaseDress uri
    How to pass an emty string to second parameter in order that it ftech endpoint from config file in same way ?

    regarsd

    Thursday, October 4, 2018 2:31 PM

All replies

  • Hi wakefun,

    A service endpoint address consists of two parts, the base address and the address of the endpoint. Here you need to configure the endpoint addresses for the service contract, and then the service host will open the listen at the specified endpoint. For example, your endpoint address is http://localhost:1500/myservice.

    You pass an empty string to the baseaddress parameter, and then you should configurate the endpoint address for the service contract.

    <services>
          <service name="Server4.MyService" behaviorConfiguration="mybev">
           <endpoint address="http://localhost:1500" binding="wsHttpBinding" contract="Server4.IService" bindingConfiguration="mybinding">
            </endpoint>
            <endpoint address=" http://localhost:1500/mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
          </service>
    </services>


    It is important to note that the address property of the endpoint can be either a relative address or an absolute address. Addresses automatically ignore different protocols. For example, the HTTP scheme base address automatically ignores the endpoint address with a net.tcp scheme url.

    Feel free to contact me if you have any questions.

    Best Regards

    Abraham

    Friday, October 5, 2018 6:09 AM
    Moderator
  • Sorry maybe I misexplain, your answer is not relative to my issue.

    My issue is based on the wa to register services with unity in order to be able to be hosted in a consol app.

    For Unity registration I am using the Unity.Wcf component which allows to do registration as below :

    protected override void ConfigureContainer(IUnityContainer container)
            {
                container
                    .RegisterType<IDataService, Method>()
                    .RegisterType<IUserRepository, UserRepository>()
                    .RegisterType<IUserData, UserData>();
    
            }
    

    With code aboc I am able to inject to my service constructor and Interface for dependency injection.

    This works great as long as I have a single interface for a service. But in my case I have a service whith 2 different contract pointing to 2 different enpoints.

    UIn order to reference those contrct using injection I should be able to do as below :

    protected override void ConfigureContainer(IUnityContainer container)
            {
                container
                    .RegisterType<IDataService, Method>()
                    .RegisterType<IUserService, Method>()
                    .RegisterType<IUserRepository, UserRepository>()
                    .RegisterType<IUserData, UserData>();
    
            }

    With this I register 2 Contract Interface with my service type named Method, I have an erro message from client app with interface maooing issue ?

    Any idea?

    Friday, October 5, 2018 12:05 PM