WCF库项目中未在Windows窗体应用程序中生成操作合同。 [英] Operation Contracts not Generated in Windows Form Application from a WCF Library Project.

查看:108
本文介绍了WCF库项目中未在Windows窗体应用程序中生成操作合同。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我创建了一个WCF库项目。我已经在WCF测试客户端测试了它,一切都按预期工作。



当我将服务添加为Windows窗体客户端的服务参考时,对于某些未知的原因,我只能得到一份操作合同(在IExam.cs中定义,名为

  bool  AddExam(...) 


这是我的合同:



IUser.cs



 使用系统; 
使用 系统集合。通用;
使用 系统 .Linq;
using System Runtime .Serialization;
使用 System .ServiceModel;
使用 System .Text;
使用 ExamGenerator;

命名空间 考试
{

[ServiceContract]
public interface IUser
{
[OperationContract]
bool 登录(string userName, string passWord);
[OperationContract]
列表<用户> GetUsers();
}

[DataContract]
public < span class =code-leadattribute> class UserData
{
[DataMember]
public string UserName;

[DataMember]
public string 密码;
}


}





IExam.cs



 使用系统; 
... // 和以前一样。

< span class =code-keyword> namespace 考试
{
[ServiceContract]
public interface IExam
{
[OperationContract]
bool AddExam( string unitCode, string unitName, String examDate, double 持续时间,
字符串说明,字符串 university , string 当然, int userID);
}

[DataContract]
public class ExamData
{
[DataMember]
public int ExamID;

[DataMember]
public string UnitCode;

[DataMember]
public string UnitName;

[DataMember]
public string ExamDate;

[DataMember]
public double 持续时间;

[DataMember]
public string 说明;

[DataMember]
public string 大学;

[DataMember]
public string 课程;

[DataMember]
public int UserID;
}
}





ISection.cs





 使用系统; 
... // 和以前一样。

< span class =code-keyword> namespace 考试
{
[ServiceContract]
public interface ISection
{
[OperationContract]
bool AddSection( string sectionName, int 标记, int examId);

列表<部分> GetSections();

}

[DataContract]
public class SectionData
{
[DataMember]
public int SectionID;

[DataMember]
public string SectionName;


[DataMember]
public int Marks ;

[DataMember]
public int ExamId;

}
}





IQuestion.cs



 使用系统; 
... // 和以前一样。

< span class =code-keyword> namespace 考试
{
[ServiceContract]
public interface IQuestion
{
[OperationContract]
bool AddQuestion( string questionPhrase, byte []图, int 标记, int sectionId, string answerPhrase, byte [] answerDiagram );

[OperationContract]
列表<问题> GetQuestions();
}

[DataContract]
public class QuestionData
{
[DataMember]
public string QuestionPhrase;
[DataMember]
public byte [] QuestionDiagram;

[DataMember]
public int 标记;

}
[DataContract]
public class 回答
{
[DataMember]
public string AnswerPhrase;
[DataMember]
public byte [] AnswerDiagram;
}
}





Service.cs



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Runtime.Serialization;
使用 System.Security.Cryptography;
使用 System.ServiceModel;
使用 System.Text;


命名空间考试
{
// 注意:您可以使用重构菜单上的重命名命令在代码和配置文件中一起更改类名Service1。
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class 服务:IUser ,IExam,ISection,IQuestion
{

public bool 登录( string userName, string 密码)
{
...
}

private UserData GetUserData( string userName)
{
...
}

public bool AddExam( string unitCode, string unitName, string examDate, double duration, string 说明,字符串 university,字符串当然, int userID)
{
...
}



private ExamData GetExamData( string unitCode)
{
...

}


public bool AddSection( string sectionName, int 标记, int examId)
{
...
}



public bool AddQuestion( string questionPhrase, byte [] questionDiagram, int 标记, int sectionId, string answerPhrase, byte [] answerDiagram)
{
...
}


public 列表<考试> GetExams()
{
...
}


public 列表<用户> ; GetUsers()
{
...
}


public 列表< Section> ; GetSections()
{
...
}


public 列表<问题> ; GetQuestions()
{
...
}
}
}





App.config 中(在服务器端),



 <?  xml    版本  =  1.0   编码  =  utf-8  >  
< 配置 >

< configSections >
<! - 有关Entity Framework配置的更多信息,请访问http:/ /go.microsoft.com/fwlink/?LinkID=237468 - >
< section name = entityFramework < span class =code-attribute> type = System.Data.Entity.Internal.ConfigFile。 EntityFrameworkSection,EntityFramework,Version = 6.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089 requirePermission = false / >

< / configSections >
< appSettings >
< add key = aspnet:UseTaskFriendlySynchronizationContext value = true / >
< / appSetti ngs >
< system.web >
< 编译 debug = true / < span class =code-keyword>>
< / system.web >
<! - 部署服务库项目时,必须将配置文件的内容添加到主机的
app.config文件中。 System.Configuration不支持库的配置文件。
- >

< system.serviceModel >
< < span class =code-leadattribute> services >
< service name = Examination.Service >
< endpoint 地址 = binding = basicHttpBinding contract = Examination.IUser >
< span class =code-keyword>< identity >
< dns value = localhost / >
< / identity >
< / endpoint >
< 端点 地址 = session binding = wsHttpBinding bindingConfiguration =

合同 = Examination.IExam / >
< < span class =code-leadattribute> endpoint address = 会话 绑定 = wsHttpBinding bindingConfiguration =

合同 = Examination.ISection / >
< endpoint 地址 = session 绑定 = wsHttpBinding bindingConfiguration =

< span class =code-attribute> 合同 = Examination.IQuestion / >
< endpoint address = mex binding = mexHttpBinding 合同 = IMetadataExchange / >
< 主机 >
< baseAddresses >
< < span class =code-leadattribute> add baseAddress = http: // localhost:8733 / Design_Time_Addresses / ExamGeneratorServices / Service / / >
< / baseAddresses >
< / host >
< / service >
< / services >
< 行为 >
< serviceBehaviors >
< 行为 >
<! - 避免披露元数据信息,
在部署前将以下值设置为false
- >

< serviceMetadata httpGetEnabled = < span class =code-attribute> httpsGetEnabled = True / >
<! - 要接收故障中的异常详细信息以进行调试,
设置值低于真。在部署
之前设置为false以避免泄露异常信息
- >

< serviceDebug includeExceptionDetailInFaults = 错误 / >
< / behavior >
< / serviceBehaviors >
< ; / behavior >
< / system.serviceModel >
< entityFramework >
< defaultConnectionFactory type = System.Data.Entity.Infrastructure.SqlConnectionFactory,EntityFramework / >
< 提供商 >
< provider invariantName < /跨度> = System.Data.SqlClient type = System.Data.Entity.SqlServer。 SqlProviderServices,EntityFramework.SqlServer / > ;
< / providers >
< / entityFramework >
< connectionStrings >
< add 名称 = ExamDBContext connectionString = metadata = res:// * / ExamDatabase.csdl | res://*/ExamDatabase.ssdl | res://*/ExamDatabase.msl; provider = System.Data.SqlClient; provider connection string = data source = (localdb)\v11.0; initial catalog = ExamGenerator;已集成 安全性 = True; MultipleActiveResultSets = True; App = EntityFramework <跨度class =code-attribute> providerName = System.Data.EntityClient < span class =code-attribute>
/ >
< / connectionStrings >
< / configuration >





客户端的App.config 中,我有:



 <?  xml     version   =  1.0     encoding   =  utf-8    >  
< configuration >
< startup >
< supportedRuntime version = v4.0 sku =\".NETFramework,Version=v4.5\" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name=\"BasicHttpBinding_IUser\" />
</basicHttpBinding>
<wsHttpBinding>
<binding name=\"WSHttpBinding_IExam\" />
<binding name=\"WSHttpBinding_ISection\" />
<binding name=\"WSHttpBinding_IQuestion\" />
</wsHttpBinding>
</bindings>
<client>
<endpoint address=\"http://localhost:8733/Design_Time_Addresses/ExamGeneratorServices/Service/\"

binding=\"basicHttpBinding\" bindingConfiguration=\"BasicHttpBinding_IUser\"

contract=\"ExamServiceReference.IUser\" name=\"BasicHttpBinding_IUser\" />
<endpoint address=\"http://localhost:8733/Design_Time_Addresses/ExamGeneratorServices/Service/session\"

binding=\"wsHttpBinding\" bindingConfiguration=\"WSHttpBinding_IExam\"

contract=\"ExamServiceReference.IExam\" name=\"WSHttpBinding_IExam\">
<identity>
<userPrincipalName value=\"CHARLES\Charles\" />
</identity>
</endpoint>
<endpoint address=\"http://localhost:8733/Design_Time_Addresses/ExamGeneratorServices/Service/session\"

binding=\"wsHttpBinding\" bindingConfiguration=\"WSHttpBinding_ISection\"

contract=\"ExamServiceReference.ISection\" name=\"WSHttpBinding_ISection\">
<identity>
<userPrincipalName value=\"CHARLES\Charles\" />
</identity>
</endpoint>
<endpoint address=\"http://localhost:8733/Design_Time_Addresses/ExamGeneratorServices/Service/session\"

binding=\"wsHttpBinding\" bindingConfiguration=\"WSHttpBinding_IQuestion\"

contract=\"ExamServiceReference.IQuestion\" name=\"WSHttpBinding_IQuestion\">
<identity>
<userPrincipalName value=\"CHARLES\Charles\" />
</identity>
</endpoint>
</client>
<
/system.serviceModel>
</configuration>





And when I create the proxy to start calling the Operation Contracts, I can only get



the Operation Contract defined:



bool AddExam(...);



Why are others not available on the client side and what can I do to make them available? I am totally stuck here.请帮我。 I have looked through the internet for a solution for this but I just can’t find any.

解决方案

you are moving on the right path, you just need to care of the following steps :



1- Make all the methods ’public’ for example your method





private UserData GetUserData(string userName) 
{
...
}





is a private method which means you wont be able to access from outside the class.



2- Make sure all operation contracts of your methods are implemented inside the interface class of the service not outside it.



make sure to browse the service file itself ’service.SVC’ and make sure that all your methods are there in the XML of it.


Hi all.

I created a WCF library project. I have tested it n the WCF Test Client and everything works just as expected.

When I add the service as a service reference for a Windows Form client, for some unknown reason, I can only get one Operation Contract (defined in IExam.cs called

bool AddExam(...) 

despite having others exposed from the service side!

Here are my contracts:

IUser.cs

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

namespace Examination
{

    [ServiceContract]
    public interface IUser
    {
        [OperationContract]
        bool Login(string userName, string passWord);
        [OperationContract]
        List<User> GetUsers();
    }

    [DataContract]
    public class UserData
    {
        [DataMember]
        public string UserName;

        [DataMember]
        public string password;
    }


}



IExam.cs

using System;
... //Just as before.

namespace Examination
{
    [ServiceContract]
    public interface IExam
    {
        [OperationContract]
        bool AddExam(string unitCode, string unitName, String examDate, double duration,
            string instructions, string university, string course, int userID);
    }

    [DataContract]
    public class ExamData
    {
        [DataMember]
        public int ExamID;

        [DataMember]
        public string UnitCode;

        [DataMember]
        public string UnitName;

        [DataMember]
        public string ExamDate;

        [DataMember]
        public double Duration;

        [DataMember]
        public string Instructions;

        [DataMember]
        public string University;

        [DataMember]
        public string Course;

        [DataMember]
        public int UserID;
    }
}



ISection.cs


using System;
... //Just as before.

namespace Examination
{
    [ServiceContract]
    public interface ISection
    {
        [OperationContract]
        bool AddSection(string sectionName,int marks, int examId);

        List<Section> GetSections();

    }

    [DataContract]
    public class SectionData
    {
        [DataMember]
        public int SectionID;

        [DataMember]
        public string SectionName;


        [DataMember]
        public int Marks;

        [DataMember]
        public int ExamId;

    }
}



IQuestion.cs

using System;
... //Just as before.

namespace Examination
{
    [ServiceContract]
    public interface IQuestion
    {
        [OperationContract]
        bool AddQuestion(string questionPhrase, byte[] diagram, int marks, int sectionId, string answerPhrase, byte[] answerDiagram);

        [OperationContract]
        List<Question> GetQuestions();
    }

    [DataContract]
    public class QuestionData
    {
        [DataMember]
        public string QuestionPhrase;
        [DataMember]
        public byte[] QuestionDiagram;

        [DataMember]
        public int Marks;

    }
    [DataContract]
    public class Answer
    {
        [DataMember]
        public string AnswerPhrase;
        [DataMember]
        public byte[] AnswerDiagram;
    }
}



Service.cs

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


namespace Examination
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class Service : IUser, IExam, ISection, IQuestion
    {

        public bool Login(string userName, string password)
        {
           ...
        }

        private UserData GetUserData(string userName)
        {
            ...
        }

        public bool AddExam(string unitCode, string unitName, string examDate, double duration, string instructions, string university, string course, int userID)
        {
           ...
        }



        private ExamData GetExamData(string unitCode)
        {
            ...

        }


        public bool AddSection(string sectionName, int marks, int examId)
        {
            ...
        }

        

        public bool AddQuestion(string questionPhrase, byte[] questionDiagram, int marks, int sectionId, string answerPhrase, byte[] answerDiagram)
        {
            ...
        }


        public List<Exam> GetExams()
        {
            ...
        }


        public List<User> GetUsers()
        {
            ...
        }


        public List<Section> GetSections()
        {
            ...
        }


        public List<Question> GetQuestions()
        {
            ...
        }
    }
}



In the App.config (on the server side),

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    
  </configSections>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="Examination.Service">
        <endpoint address="" binding="basicHttpBinding" contract="Examination.IUser">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="session" binding="wsHttpBinding" bindingConfiguration=""

          contract="Examination.IExam" />
        <endpoint address="session" binding="wsHttpBinding" bindingConfiguration=""

          contract="Examination.ISection" />
        <endpoint address="session" binding="wsHttpBinding" bindingConfiguration=""

          contract="Examination.IQuestion" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8733/Design_Time_Addresses/ExamGeneratorServices/Service/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="ExamDBContext" connectionString="metadata=res://*/ExamDatabase.csdl|res://*/ExamDatabase.ssdl|res://*/ExamDatabase.msl;provider=System.Data.SqlClient;provider connection string="data source=(localdb)\v11.0;initial catalog=ExamGenerator;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>



In the client's App.config, I have:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IUser" />
            </basicHttpBinding>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IExam" />
                <binding name="WSHttpBinding_ISection" />
                <binding name="WSHttpBinding_IQuestion" />
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8733/Design_Time_Addresses/ExamGeneratorServices/Service/"

                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IUser"

                contract="ExamServiceReference.IUser" name="BasicHttpBinding_IUser" />
            <endpoint address="http://localhost:8733/Design_Time_Addresses/ExamGeneratorServices/Service/session"

                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IExam"

                contract="ExamServiceReference.IExam" name="WSHttpBinding_IExam">
                <identity>
                    <userPrincipalName value="CHARLES\Charles" />
                </identity>
            </endpoint>
            <endpoint address="http://localhost:8733/Design_Time_Addresses/ExamGeneratorServices/Service/session"

                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ISection"

                contract="ExamServiceReference.ISection" name="WSHttpBinding_ISection">
                <identity>
                    <userPrincipalName value="CHARLES\Charles" />
                </identity>
            </endpoint>
            <endpoint address="http://localhost:8733/Design_Time_Addresses/ExamGeneratorServices/Service/session"

                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IQuestion"

                contract="ExamServiceReference.IQuestion" name="WSHttpBinding_IQuestion">
                <identity>
                    <userPrincipalName value="CHARLES\Charles" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>



And when I create the proxy to start calling the Operation Contracts, I can only get

the Operation Contract defined:

bool AddExam(...);

Why are others not available on the client side and what can I do to make them available? I am totally stuck here. Please help me. I have looked through the internet for a solution for this but I just can't find any.

解决方案

you are moving on the right path, you just need to care of the following steps :

1- Make all the methods 'public' for example your method


private UserData GetUserData(string userName)
       {
           ...
       }



is a private method which means you wont be able to access from outside the class.

2- Make sure all operation contracts of your methods are implemented inside the interface class of the service not outside it.

make sure to browse the service file itself 'service.SVC' and make sure that all your methods are there in the XML of it.


这篇关于WCF库项目中未在Windows窗体应用程序中生成操作合同。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆