开发者

Trying to start the first WCF service

开发者 https://www.devze.com 2023-03-05 11:10 出处:网络
I am trying to start my first WCF service. Well, I want to point out that I have fully understood the WCF architecture and pillars (ABC: Address Binding and Contract = Endpoint). Furthermore, I have

I am trying to start my first WCF service.

Well, I want to point out that I have fully understood the WCF architecture and pillars (ABC: Address Binding and Contract = Endpoint). Furthermore, I have understood many elements of the WCF philosophy, so, I am not exactly a mere newbye...

However, theory aside, real problems come when someone puts his hands on the real things...

I have these three files:

File IService1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Service开发者_运维问答Model;
using System.Text;

/// <summary>
/// This is the interface that specifies contract for the Sevice1 of this service application.
/// In this file the interface is specified in order to set the service operations that can be invoked by requestors.
/// </summary>
namespace EchoWcfLibrary {
    /// <summary>
    /// The interface specifies for those classes implementing it (services), the operation that the service will expose.
    /// </summary>
    [ServiceContract]
    public interface IService1 {
        // This does not use serialization (implicit serialization in considered: base types used).
        [OperationContract]
        string GetData(int value);
        // This uses data contracts and serialization.
        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
    }

    /// <summary>
    /// The following class defines data contract for those operations managing with non primitive types and, for this reason, needing serialization support (explicit, not implicit)
    /// </summary>
    [DataContract]
    public class CompositeType {
        // Members not serialized
        bool boolValue = true;
        string stringValue = "Hello ";
        // Serialized
        [DataMember]
        public bool BoolValue {
            get { return boolValue; }
            set { boolValue = value; }
        }
        // Serialized
        [DataMember]
        public string StringValue {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}

File Service1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

/// <summary>
/// This is the service host implementation. A class implementing the service is specified.
/// </summary>
namespace EchoWcfLibrary {
    /// <summary>
    /// This class implements the IService1 service.
    /// </summary>
    public class Service1 : IService1 {
        // One operation.
        public string GetData(int value) {
            return string.Format("You entered: {0}", value);
        }
        // The other operation.
        public CompositeType GetDataUsingDataContract(CompositeType composite) {
            if (composite == null) {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue) {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}

These files, are placed inside a project called EchoWcfLibrary

And the main: Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using EchoWcfLibrary;

namespace WcfServiceApplication {
    public static class Program {
        static void Main(string[] args) {
            // Setting endpoints and setting the service to start properly.
            // Base address specified: http://localhost:8080/service1
            using (ServiceHost host = new ServiceHost(typeof(Service1), new Uri("http://localhost:8080/service1"))) {
                host.AddServiceEndpoint(typeof(IService1), new BasicHttpBinding(), "svc");
                host.AddServiceEndpoint(typeof(IService1), new NetTcpBinding(), "net.tcp://localhost:8081/service1/tcpsvc");
                host.Open();
                System.Threading.Thread.Sleep(1000000);
                host.Close();
            }
        }
    }
}

This last file is in a separate project called WcfServiceApplication The two projects exist in the same solution. The WcfServiceApplication has, of course, a link to the other project.

I would like to start this service that, as you can see, is the one that Visual Studio puts in the WCF Library template.

Well, I tried to run it the first times and had some problems with http namespace reservations, I fixed it using netsh and adding explicit reservations for my user and for the specified http namespaces.

However what I encounter is the following: The WCF host application, which is a small application, very useful, shows the currently hosted services. There is just one service hosted: mine but its status is stopped, and it tells me, in the description box, that NO ENDPOINT has been defined!!!

But I defined them in Program.cs... I do not understand... What am I doing wrong?

Thankyou

PS Note that even defining host.AddServiceEndpoint(typeof(IService1), new BasicHttpBinding(), "svc") only (without the tcp endpoint) gives the same result....

One more thing: I understand that this approach in building a service is not very good... however, instead using auto-generating code tools I would like to understand, first, how to create and run a service from the roots, and then, how to do that with higher level tools... thank you


The problem appears to be that you have defined two endpoints, 'svc' (http) and 'net.tcp://localhost:8081/service1/tcpsvc' (tcp), and then tried to start the service host using a third endpoint, which is not defined in either of the two endpoints you've configured.

I recommend removing the code that is programmatically creating the bindings, adding a .config file to your project, and then using the WCF Service Configuration Editor built into Visual Studio (as of 2008 onwards) to do the heavy-lifting for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消