0

I am trying to use the Ninject Dependency Injection to bind a callback method to WCF REST services runs in a kind of plugin module of a software system, which is NOT possible to use the SVC file or webconfig or app.config for any configuration.

The interface and implementation of the WCF services are defined as below:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    string DoWork();
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class Service1 : IService1
{
    private IEventCallback EventCallback { get; set; }

    public Service1(IEventCallback eventCallback)
    {
        EventCallback = eventCallback;
    }

    public string DoWork()
    {
        if (EventCallback != null)
        {
            EventCallback.Send("Testing Event ID");    
        }            

        return "Success";
    }
}

where the IEventCallback and the corresponding implementation are defined as below:

public interface IEventCallback
{
    void Send(string eventId);
}

public class EventCallback : IEventCallback
{
    private Action<string> OnSendCustomEventCallBack { get; set; }

    public EventCallback(Action<string> onSendCustomEventCallBack)
    {
        OnSendCustomEventCallBack = onSendCustomEventCallBack;
    }

    public void Send(string eventId)
    {
        if (OnSendCustomEventCallBack != null)
        {
            OnSendCustomEventCallBack(eventId);
        }
    }
}

The codes to create the REST Service are as below:

public AuthenticatedWebServiceHost(Type type, Uri url, string authenUsername, string authenPassword)
{
    AuthenUsername = authenUsername;
    AuthenPassword = authenPassword;

    IDictionary<string, ContractDescription> desc;

    InitializeDescription(type, new UriSchemeKeyedCollection());
    base.CreateDescription(out desc);
    var val = desc.Values.First();            

    var binding = new WebHttpBinding();
    binding.Security.Mode = WebHttpSecurityMode.TransportCredentialOnly;
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

    Credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
    Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = 
        new CustomUserNamePasswordValidator(AuthenUsername, AuthenPassword);

    AddServiceEndpoint(val.ContractType, binding, url);
}

And, the AuthenticatedWebServiceHost is called as below:

var eventCallback = new EventCallback(OnSendCustomEventCallBack);   // where OnSendCustomEventCallBack is a defined method   

// How to write codes to use Ninject to inject the callback into the Service?
// kernel.Bind<IEventCallback>().To<??>()

_webServiceHost = new AuthenticatedWebServiceHost(typeof(Service1), new Uri("http://localhost:9000/Events"),
    "admin", "password");

_webServiceHost.Open();

Since, no XML configuration is allowed in my case, how to write codes to use Ninject to bind the callback to the WCF Services?

user1133555
  • 103
  • 8

1 Answers1

0

I eventually figured out the solution by referencing Is it possible to instantiate a WebServiceHost via an instance of the service type, without a parameterless constructor? without using NInject.

user1133555
  • 103
  • 8