c# - Windows Communication Foundation Null Reference Error -
i trying use windows communication foundation(wcf) testing purposes in application , getting null object. new using wcf, appreciated.
from understand, each host has 1 service type(class). have several interfaces , classes want test. goal make 1 master class has instances of others, don't have have several hosts running. below of code.
host-
public class automationwcfhost { public servicehost host; public automationwcfhost() { uri httpurl = new uri("http://localhost:8090/myfun"); host = new servicehost(typeof(myfun), httpurl); servicemetadatabehavior smb = new servicemetadatabehavior(); smb.httpgetenabled = true; smb.metadataexporter.policyversion = policyversion.policy15; host.description.behaviors.add(smb); } public void startwcf() { this.host.open(); } public void stopwcf(){ this.host.close();
interfaces -
[servicecontract] public interface imyfun { [operationcontract] string echo(string msg); idatasources datasources { [operationcontract]get; [operationcontract]set; } } [servicecontract] public interface idatasources { [operationcontract] void add(datasource datasource); [operationcontract] void remove(datasource datasource); }
class - trying make 1 base class don't have have multiple hosts running.
public class myfun : imyfun { public datasources _datasources; public myfun () { _datasources = new datasources(); } public string echo(string msg) { return msg; } public idatasources datasources { { return _datasources; } set {_datasources = (datasources)value;} } } public class datasources : idatasources{ ....//several methods , properties }
test -
public void mytest() { endpointaddress address = new endpointaddress("http://localhost:8090/myfun"); basichttpbinding binding = new basichttpbinding(); channelfactory<imyfun> factory = new channelfactory<imyfun>(binding, address); imyfun channel = factory.createchannel(); assert.areequal("test", channel.echo("test")); idatasources datasources = channel.datasources; datasources.add(newdatasource); }
as step through, runs echo method fine, when does
idatasources datasources = channel.datasources;
datasources null. makes
datasources.add(newdatasource);
fails due null exception error.
Comments
Post a Comment