crm高级开发人员工具与WCF无法正常工作

时间:2012-02-29 08:14:43

标签: wcf ado.net dynamics-crm

我创建了一个简单的wcf服务,它使用高级开发人员工具从crm 4.0中检索数据。我使用linq成功查询了数据。这些类是使用crmsvcutil生成的。但是,当我将其转换为wcf服务时,它会一直崩溃。

namespace CRMDataRetrieval
 {    
 [ServiceContract]
public interface ICRMData
{
    [OperationContract]
    string getValue();

}

public class CRMDataService : ICRMData
{
    public string getValue()
    {
        DataContext context = new DataContext("entities");  //entities is name of    classes that were generated by crmsvcutil

        string name = null;
        var query = from n in context.contacts
                    where n.acctNum == "01218515"
                    select n.nickname;
        foreach (var result in query)
            name = result;
        return name;
    }

在WCF服务主机中,服务已停止,但它也显示Entities.CmsDataService也已停止。当我点击它时,附加信息表明无法启动服务(Entities.CmsDataService)。此服务没有定义端点。请在配置文件中为服务添加至少一个端点,然后重试。

我的app.config看起来如下所示。 那么我如何以及在何处在自动生成的类的配置文件中添加端点,以便它们与WCF一起很好地工作?或者我需要进行任何其他更改? 请在解释中详细说明。像往常一样,提前感谢你。

    <?xml version="1.0"?>
<configuration>
  <configSections>
  </configSections>
  <connectionStrings>
    <add name="entities" connectionString="Authentication Type=ad; Server=http://****; User ID=*\*******; Password=*******"/>
  </connectionStrings>
  <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>
    <bindings />
    <client />
    <services>
      <service name="CRMDataRetrieval.CRMDataService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:7432/account"/>
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint name="wsHttpBinding_ICRMData" address="ws" binding="wsHttpBinding" contract="CRMDataRetrieval.ICRMData"/>


        <!-- Metadata Endpoints -->
        <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. --> 
        <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="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>
      <endpointBehaviors>

      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

<startup><supportedRuntime version="v2.0.50727"/></startup></configuration>

2 个答案:

答案 0 :(得分:0)

你能提出一个hello world端点并使用它吗?完成后,您可以使用已编写的工作代码。 (除非crmsvcutil生成一些专门用于托管wcf端点的类,我不知道?)

我同意paramosh - 'Entities.CmsDataService'不是我希望给你的例子服务的名称。我原本期望'CRMDataRetrieval.CRMDataService'

答案 1 :(得分:0)

看看crmsvcutil parameters。有/ dataContextPrefix和/ dataContextClassName。使用这些参数为生成的数据上下文设置正确的名称。使用生成的上下文,如here

所述
CrmConnection crmc = CrmConnection.Parse("Authentication Type=Passport; Server=https://" + org + ".crm.dynamics.com/" + org + "; User ID=myuser; Password=mypassword; Device ID=mydeviceid; Device Password=mydevicepassword");
var yourDataContext = new GeneratedDataContext(crmc);

不幸的是我没有安装CRM 4.0。但我可以为CRM 2011提供代码:

namespace CRMDataRetrieval
{
    public class CRMDataService : ICRMData
    {
        public string getValue()
        {
            var connection = CrmConnection.Parse("Url=https://orgname.crm.dynamics.com; Username=OpenId; Password=; DeviceID=####; DevicePassword=####");
            var service = new OrganizationService(connection);
            MyServiceContext context = new MyServiceContext(service);

            string name = null;
            var query = from n in context.ContactSet
                        where n.ContactId == new Guid("00000000-0000-0000-0000-000000000000")
                        select n.FullName;
            foreach (var result in query)
                name = result;

            return name;
        }
    }
}

我使用了您提供的端点配置。

生成我用过的课程

crmsvcutil.exe /url:https://orgname.crm.dynamics.com/XRMServices/2011/Organization.svc /out:GeneratedCode.cs /n:MyCrmNamespace /u:"OpenId" /p:"####" /serviceContextName:MyServiceContext /di:#### /dp:####