WCF初體驗Bug:No code was generated

摘要:WCF初體驗Bug:No code was generated

由於工作關係而第一次寫WCF(Windows Communication Foundation)
 
一開始參考
 
http://www.dotblogs.com.tw/mis2000lab/archive/2010/09/16/wcf_02_diy.aspx
 
寫第一支程式遇到的Bug如下:
 
No code was generated 等後面一長串
 
主要原因為名稱不同無法對應:
 
1.Service2.svc
 
<%@ ServiceHost Language="C#" Debug="true" Service="FirstWcfService.Service2" CodeBehind="Service2.svc.cs" %>
 
2.Service2.svc.cs
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
 
namespace FirstWcfService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service2 : IService2//IService2不能少
 
    {
        public string GetData(int value)
        {
            return string.Format("你輸入了: {0}", value);
        }
 
        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        } 
 
        public String DoWork()
        {
            return "您好!我來了!";
        }
    }
}
 
3.IService2.cs
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
 
namespace FirstWcfService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService2
    {
 
        [OperationContract]
        string GetData(int value);
 
        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
 
        // TODO: Add your service operations here
 
        [OperationContract]
        string DoWork();
    }
 
 
    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";
 
        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }
 
        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}
在此記錄下來