IService1.cs
添加的接口
1 [OperationContract] 2 [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 3 TranslateResult jsonData2(FileData data);
定义的 数据复杂类型
// 使用下面示例中说明的数据约定将复合类型添加到服务操作。 [DataContract] public class FileData { [DataMember(Name = "FileName")] public string FileName { get; set; } [DataMember(Name = "Length")] public long Length { get; set; } [DataMember(Name = "DeviceName")] public string DeviceName { get; set; } [DataMember(Name = "UserType")] public string UserType { get; set; } [DataMember(Name = "SourceLanguage")] public string SourceLanguage { get; set; } [DataMember(Name = "TargetLanguage")] public string TargetLanguage { get; set; } [DataMember(Name = "FileStreamContent")] public string FileStreamContent { get; set; } } [DataContract] public class TranslateResult { [DataMember(Name = "ErrMessage")] public string ErrMessage { get; set; } [DataMember(Name = "IsSuccess")] public bool IsSuccess { get; set; } [DataMember(Name = "OcrResult")] public string OcrResult { get; set; } [DataMember(Name = "TransResult")] public string TransResult { get; set; } [DataMember(Name = "procTime")] public string procTime { get; set; } }
Service1.svc 中的接口实现
public TranslateResult jsonData2(FileData data) { TranslateResult result = new TranslateResult(); if (data == null || data.FileStreamContent == null) { result.IsSuccess = false; result.ErrMessage = "参数无效"; return result; } try {
byte[] bytes = Convert.FromBase64String(data.FileStreamContent); MemoryStream ms = new MemoryStream(bytes); string ocrResult = OcrPicOperation(ms); result.OcrResult = ocrResult; result.TransResult = TransTargetLanguage(ocrResult, "zh-CHS"); result.IsSuccess = true; } catch (Exception ex) { result.ErrMessage = ex.Message; result.IsSuccess = false; } return result; }
Web.config
//原来的配置被我注释了,在下面。
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.web> <compilation debug="true" /> </system.web> <system.serviceModel> <services> <service name="WcfServiceWeb.OCRTranslationService" behaviorConfiguration="android"> <endpoint address="" binding="webHttpBinding" behaviorConfiguration="webHttp" contract="WcfServiceWeb.IOCRTranslationService" /> </service> </services> <client> <endpoint address="http://api.microsofttranslator.com/V2/soap.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_LanguageService" contract="Translation.LanguageService" name="BasicHttpBinding_LanguageService" /> </client> <!--添加对上传文件大小的限制--> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_LanguageService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:05:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="104857600" maxReceivedMessageSize="2147483647" transferMode="StreamedRequest" useDefaultWebProxy="true"> <readerQuotas maxDepth="3200" maxStringContentLength="104857600" maxArrayLength="104857600" maxBytesPerRead="104857600" maxNameTableCharCount="104857600" /> <security mode="None"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> </security> </binding> </basicHttpBinding> <wsHttpBinding> <binding name="wsHttp" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Mtom"> <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> <security mode="None" /> </binding> </wsHttpBinding> </bindings> <behaviors> <endpointBehaviors > <behavior name="webHttp"> <webHttp helpEnabled="true" /> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior name="android"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> <dataContractSerializer maxItemsInObjectGraph="2147483647"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true" /> <directoryBrowse enabled="true" /> </system.webServer> </configuration> <!--<?xml version="1.0" encoding="utf-8"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_LanguageService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:05:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="104857600" maxReceivedMessageSize="2147483647" transferMode="StreamedRequest" useDefaultWebProxy="true"> <readerQuotas maxDepth="3200" maxStringContentLength="104857600" maxArrayLength="104857600" maxBytesPerRead="104857600" maxNameTableCharCount="104857600" /> <security mode="None"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> </security> </binding> </basicHttpBinding> <wsHttpBinding> <binding name="wsHttp" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Mtom"> <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> <security mode="None" /> </binding> </wsHttpBinding> </bindings> <services> <service name="WcfServiceWeb.OCRTranslationService" behaviorConfiguration="HttpGetBehavior"> <endpoint binding="wsHttpBinding" bindingConfiguration="wsHttp" contract="WcfServiceWeb.IOCRTranslationService" address=""/> </service> </services> <client> <endpoint address="http://api.microsofttranslator.com/V2/soap.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_LanguageService" contract="Translation.LanguageService" name="BasicHttpBinding_LanguageService" /> </client> <behaviors> <serviceBehaviors> <behavior name="HttpGetBehavior"> --><!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 --><!-- <serviceMetadata httpGetEnabled="true"/> --><!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 --><!-- <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration>-->