Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Client Assembly:
- void WCFClientChannel()
- {
- var wcfClient = new ObjectManagerClient();
- var simpleObject = wcfClient.GetSimpleObject(0) as ISimpleObject;// this cath exeption
- simpleObject.TestCall();
- }
- //Common Assembly:
- public interface ISimpleObject
- {
- string TestCall();
- }
- [ServiceContract]
- public interface IObjectManager
- {
- [OperationContract]
- [ServiceKnownType(typeof(ISimpleObject))]
- ISimpleObject GetSimpleObject(int id);
- }
- //SimpleObject Assembly:
- [DataContract(Name = "SimpleObject")]
- [KnownType(typeof(ISimpleObject))]
- [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, IncludeExceptionDetailInFaults = true)]
- public class SimpleObject : MarshalByRefObject, ISimpleObject, IDisposable
- {
- public SimpleObject(int Id)
- {
- this.Id = Id;
- }
- public string TestCall()
- {
- var watch = new Stopwatch();
- watch.Start();
- Console.Write("TestCall ");
- watch.Stop();
- var time = watch.Elapsed.ToString("ss\.ffffff") + " sec";
- Console.WriteLine(time);
- return string.Format("Time: {0}", time);
- }
- [DataMember(Name = "Id")]
- public int Id { get; private set; }
- public void Dispose()
- {
- //throw new NotImplementedException();
- }
- //ObjectManager Assembly:
- [KnownType(typeof(ObjectManager))]
- [DataContract(Name = "ObjectManager")]
- public class ObjectManager : SimpleObject, IObjectManager
- {
- public ObjectManager()
- {
- if (components == null || !components.Any())
- {
- components = new List<SimpleObject>
- {
- new SimpleObject(0),
- new SimpleObject(1),
- };
- }
- }
- public ISimpleObject GetSimpleObject(int id)
- {
- if (components.Any(o => o.Id == id))
- {
- Console.WriteLine("Return SimpleObject with id {0}", id);
- ISimpleObject o = components[id];
- return o;
- }
- throw new KeyNotFoundException(string.Format("can not find object with id = {0}", Id));
- }
- private readonly List<SimpleObject> components;
- }
- //Service Assembly:
- private void InitializeWCF()
- {
- Console.WriteLine("Opening WCF host...");
- var baseAddress = new Uri("http://localhost:8000/ServiceManager");
- wcfHost = new ServiceHost(typeof(ObjectManager), baseAddress);
- try
- {
- wcfHost.AddServiceEndpoint(
- typeof(IObjectManager),
- new WSHttpBinding()
- {
- ReceiveTimeout = TimeSpan.FromMinutes(15),
- OpenTimeout = TimeSpan.FromMinutes(1),
- SendTimeout = TimeSpan.FromMinutes(30),
- MaxReceivedMessageSize = Int32.MaxValue,
- Security = new WSHttpSecurity(){Transport = new HttpTransportSecurity(){ClientCredentialType = HttpClientCredentialType.Windows}}
- },
- "ObjectManager");
- var smb = new ServiceMetadataBehavior { HttpGetEnabled = true };
- wcfHost.Description.Behaviors.Add(smb);
- //System.Net.ServicePointManager.Expect100Continue = false;
- wcfHost.Open();
- Console.WriteLine("WCF host is ready.");
- }
- catch (CommunicationException ce)
- {
- Console.WriteLine("WCF host is failed. An exception occurred: {0}", ce.Message);
- wcfHost.Abort();
- }
- finally
- {
- Console.WriteLine(Environment.NewLine);
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <configuration>
- <system.serviceModel>
- <bindings>
- <wsHttpBinding>
- <binding name="WSHttpBinding_IObjectManager" />
- </wsHttpBinding>
- </bindings>
- <client>
- <endpoint address="http://localhost:8000/ServiceManager/ObjectManager"
- binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IObjectManager"
- contract="IObjectManager" name="WSHttpBinding_IObjectManager">
- </endpoint>
- </client>
- </system.serviceModel>
- </configuration>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement