기본적인 형태의 프로젝트 구조

 

계약된 웹서비스를 만들기 위한 인터페이스

using System.ServiceModel;
using System.ServiceModel.Description;

namespace IChannel
{
    [ServiceContract]
    public  interface IHelloWorld
    {
        [OperationContract]
        string Hellworld();
    }
}

 

웹서비스(서버측)

using IChannel;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1 , IChannel.IHelloWorld
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        string IHelloWorld.Hellworld()
        {
            return "TEST MODE";
        } 
    }
}

 

클라이언트에서 호출

Icannerl 이 참조되어져 있습니다.

string uri = "http://localhost:49724/Service1.svc";

ServiceEndpoint ep = new ServiceEndpoint(ContractDescription.GetContract(typeof(IChannel.IHelloWorld)), new wsHttpBinding(), new EndpointAddress(uri));
ChannelFactory<IChannel.IHelloWorld> factory = new ChannelFactory<IChannel.IHelloWorld>(ep);
IChannel.IHelloWorld proxy = factory.CreateChannel();

string result = proxy.Hellworld();
(proxy as IDisposable).Dispose();

MessageBox.Show(result);      


서버측 웹컨피그 수정 
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MarinerERP.WebService.DataService.InsaServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="MarinerERP.WebService.DataService.InsaServiceBehavior"
        name="MarinerERP.WebService.DataService.InsaService">
        <endpoint address="" binding="wsHttpBinding" contract="IChannel.IHelloWorld "> //인터페이스로 맞추어주어야 합니다.
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>


contract 는 웹서비스와 클라이언트와의 계약이기 때문에 꼭 맞추어 주세요!

 

 

+ Recent posts