第三方调用U9服务(DotNet平台)一、业务场景PLM(PDM)系统和U9ERP系统整合,PLM设计完产品后需要传入到U9ERP系统,比如要传入厂牌,料品,BOM等基础信息二、引用U9服务以创建料品服务为例,UFIDA.U9.ISV.Item.BatchCreateItemByDTOSRVa)验证服务是否可用如果能显示显示下面的图片说明服务没有问题b)生成代理类若使用VisualStudio2005则需要先下载SvcUtil.exe在命令行输入SvcUtil:DataContractSerializer如下图所示:输入命令后输出的不上上图格式则重新再试或查看第一步服务地址是否正确c)构架项目1.用VS新建项目,2.把代理类和配置文件加到项目中3.修改output.config名称为app.config4.添加System.Runtime.Serialization.dll和System.ServiceModel.dll引用如下图所示如果一个项目中需要用到调用多个服务,则需要合并代理类和配置文件,请参考合并代理类说明文档三、项目实现细节a)调用服务框架代码//实例化代理类UFIDAU9ISVItemIBatchCreateItemByDTOSRVClientclient;client=newUFIDAU9ISVItemIBatchCreateItemByDTOSRVClient();UFIDAU9CBOSCMItemItemMasterData[]returnItems;//服务返回结果UFSoft.UBF.Exceptions.MessageBase[]returnMsg;//返回异常信息,目前还没有使用此属性objectcontext;//上下文信息UFIDAU9ISVItemItemMasterDTOData[]itemMasterDtos;//传入参数try{context=CreateContextObj();//给上下文信息赋值itemMasterDtos=SetItemMasterDtos();//给传入参数赋值returnItems=client.Do(outreturnMsg,context,itemMasterDtos);//服务调用}catch(Exceptionex){//异常信息捕获thrownewException(GetExceptionMessage(ex));}b)上下文信息///summary///给上下文信息赋值////summary///returns/returnsprivatestaticThreadContextCreateContextObj(){//实例化应用上下文对象ThreadContextthContext=newThreadContext();System.Collections.Generic.Dictionaryobject,objectns=newDictionaryobject,object();ns.Add(OrgID,1000911212125298);//组织ns.Add(UserID,1000911217626500);//用户ns.Add(CultureName,zh-CN);//语言ns.Add(EnterpriseID,007);//企业thContext.nameValueHas=ns;returnthContext;}c)传入参数///summary///给传入参数赋值////summary///returns/returnsprivatestaticUFIDAU9ISVItemItemMasterDTOData[]SetItemMasterDtos(){ListUFIDAU9ISVItemItemMasterDTODatadtos=newListUFIDAU9ISVItemItemMasterDTOData();foreach(i….{UFIDAU9ISVItemItemMasterDTODatadto=newUFIDAU9ISVItemItemMasterDTOData();//为料品dto赋值dto.m_code=1-01-+i.ToString();dto.m_name=自建料品+i.ToString();dto.m_status=SetCommonArchiveDTO(3200);//状态码(关联实体类型赋值)dto.m_inventoryUOM=SetCommonArchiveDTO(001);//库存主单位dto.m_itemFormAttribute=10;//制造件//描述性弹性域UFIDAU9BaseFlexFieldDescFlexFieldDescFlexSegmentsDatadescFlexSegs=newUFIDAU9BaseFlexFieldDescFlexFieldDescFlexSegmentsData();descFlexSegs.m_privateDescSeg1=英文描述等;dto.m_descFlexField=descFlexSegs;…省略部分代码//把料品dto添加到集合中dtos.Add(dto);}returndtos.ToArray();}d)提取服务异常如果还有异常提取有问题,请开发人员跟踪检查异常信息结构,在修改下面的方法///summary///提取异常信息////summary///paramname=ex/paramprivatestaticstringGetExceptionMessage(Exceptionex){stringfaultMessage=未知错误,请查看ERP日志!;System.TimeoutExceptiontimeoutEx=exasSystem.TimeoutException;if(timeoutEx!=null){faultMessage=因第一次访问ERP服务,访问超时,如避免此错误,请先启动ERP系统!;}else{FaultExceptionServiceExceptionfaultEx=exasFaultExceptionServiceException;if(faultEx==null){faultMessage=ex.Message;}else{ServiceExceptionserviceEx=faultEx.Detail;if(serviceEx!=null&&!string.IsNullOrEmpty(serviceEx.Message)&&!serviceEx.Message.Equals(fault,StringComparison.OrdinalIgnoreCase)){//错误信息在faultEx.Message中,请提取,//格式为Fault:料品不能为空,请录入\n在.....intstartIndex=serviceEx.Message.IndexOf(:);intendIndex=serviceEx.Message.IndexOf(\n);if(endIndex==-1)endIndex=serviceEx.Message.Length;if(endIndex0&&endIndexstartIndex+1){faultMessage=serviceEx.Message.Substring(startIndex+1,endIndex-startIndex-1);}else{faultMessage=serviceEx.Message;}}}}returnfaultMessage;}e)公共方法///summary///为关联实体属性赋值////summary///paramname=entityID实体记录ID/param///returns/returnspublicstaticUFIDAU9CBOPubControllerCommonArchiveDataDTODataSetCommonArchiveDTO(longentityID){returnSetCommonArchiveDTO(entityID,string.Empty);}///summary///为关联实体属性赋值////summary///paramname=entityCode实体记录编码/param///returns/returnspublicstaticUFIDAU9CBOPubControllerCommonArchiveDataDTODataSetCommonArchiveDTO(stringentityCode){returnSetCommonArchiveDTO(-1,entityCode);}///summary///为关联实体属性赋值////summary///paramname=entityID实体记录ID/param///paramname=entityCode实体记录编码/param///returns/returnspublicstaticUFIDAU9CBOPubControllerCommonArchiveDataDTODataSetCommonArchiveDTO(longentityID,stringentityCode){UFIDAU9CBOPubControllerCommonArchiveDataDTODatadto=newUFIDAU9CBOPubControllerCommonArchiveDataDTOData();if(entityID=0&&string.IsNullOrEmpty(entityCode))returnnull;dto.m_code=entityCode;dto.m_iD=entityID;returndto;}请参考案例代码四、项目配置信息属性说明:receiveTimeout:调用服务时超时时间maxReceivedMessageSize:传入的参数大小address:调用的服务地址bindingConfiguration:绑定的配置名称bindingname=”配置名称”下面是自动生成的配置文件bindingsbasicHttpBindingbindingname=BasicHttpBinding_UFIDA.U9.ISV.Item.IBatchCreateItemByDTOSRVcloseTimeout=00:01:00openTimeout=00:01:00receiveTimeout=00:10:00sendTimeout=00:01:00allowCookies=falsebypassProxyOnLocal=falsehostNameComparisonMode=StrongWildcardmaxBufferSize=65536maxBufferPoolSize=524288maxReceivedMessageSize=65536messageEncoding=TexttextEncoding=utf-8transferMode=BuffereduseDefaultWebProxy=truereaderQuotasmaxDepth=32maxStringContentLength=8192maxArrayLength=16384maxBytesPerRead=4096maxNameTableCharCount=16384/securitymode=NonetransportclientCredentialType=NoneproxyCredentialType=Nonerealm=/mes