C#WCF Web服务最佳实践

时间:2017-06-09 15:42:05

标签: c# web-services wcf

我对于开发相对较新,而且我主要是自学成才,所以我所知道的很多内容都来自google搜索和从那里搞清楚事情!

话虽如此,我已经整理了一个ASP MVC WebApp,它充当了一个非常有效的资产管理系统。

现在我想将其扩展到移动设备;但是它需要离线工作,只需要少量的功能。

我计划聘请一位Android开发人员为我整理一些可以利用我使用的Web服务的东西,但在我开始这样做之前,我想问一下是否有人可以看看我做过的事情,只是告诉我,如果我忽略了什么重大的......它是有用的,但我知道它不符合行业标准,我想改进。

我非常感谢有关错误/不适合用它的一些反馈。我应该集中精力改善这一点。

谢谢!

IWorkSvc.cs:

[ServiceContract]
public interface IWorkSvc
{
    [OperationContract]
    WebServiceResult AddEditWork(FmsWork w);

    [OperationContract]
    List<WSRWorkReason> ListWorkReasons();

    [OperationContract]
    List<WSRVehicle> ListVehicles(string reg = null);

    [OperationContract]
    WebServiceResult MobileLogin(string userName, string password);
}

WorkSvc.svc:

 public class WorkSvc : IWorkSvc
   {
    readonly IRepository<Work> _workRepo;
    readonly IRepository<SecurityGroup> _secRepo;
    readonly IRepository<GroupPermission> _groupRepo;
    readonly IRepository<WorkItem> _itemRepo;
    readonly IRepository<Reason> _reasonRepo;
    readonly IRepository<Vehicle> _vehicleRepo;
    readonly IRepository<LogonTracker> _logonRepo;
    readonly IUnitOfWork _uow;

    public WorkSvc()
    {
        IUnitOfWork uow = new UnitOfWork<FMSEntities>();
        _uow = uow;
        _workRepo = _uow.GetRepository<Work>();
        _secRepo = uow.GetRepository<SecurityGroup>();
        _groupRepo = uow.GetRepository<GroupPermission>();
        _itemRepo = _uow.GetRepository<WorkItem>();
        _reasonRepo = _uow.GetRepository<Reason>();
        _vehicleRepo = _uow.GetRepository<Vehicle>();
        _logonRepo = uow.GetRepository<LogonTracker>();
    }

    public List<WSRWorkReason> ListWorkReasons()
    {
        DataTable dt = new DataTable();

        var data = _reasonRepo.Find(x => x.Active).ToList();
        var refinedData = data.Select(x => new WSRWorkReason()
        {
            ReasonId = x.Id,
            Reason = x.ReasonDescriptor
        }).ToList();

        return refinedData;
    }

    public List<WSRVehicle> ListVehicles(string reg = null)
    {
        List<Vehicle> vehicleList = new List<Vehicle>();

        if (string.IsNullOrEmpty(reg))
        {
            vehicleList = _vehicleRepo.Find(x => x.Sold != true && x.Disposed != true).OrderBy(x => x.Registration)
                .ToList();
        }
        else
        {
            vehicleList = _vehicleRepo
                .Find(x => x.Sold != true && x.Disposed != true && x.Registration.Contains(reg.ToUpper()))
                .OrderBy(x => x.Registration).ToList();
        }

        var refinedData = vehicleList.Select(x => new WSRVehicle()
        {
            VehicleId = x.Id,
            Reg = x.Registration
        }).ToList();

        return refinedData;
    }

    public WebServiceResult AddEditWork(FmsWork w)
    {
        WebServiceResult wsr = new WebServiceResult()
        {
            Type = "Work",
            Success = false,
            Message = "",
        };

        try
        {
            if (w != null)
            {
                Work work = new Work()
                {
                    Id = w.Id,
                    VehicleId = w.VehicleId,
                    HoursOfLabour = w.HoursOfLabour,
                    Description = w.Description,
                    Date = w.Date,
                    Notes = w.Notes,
                    ReasonId = w.ReasonId,
                    CreatedOn = DateTime.Now,
                    CreatedBy = w.CreatedBy,
                    ModifiedBy = w.ModifiedBy,
                    ModifiedOn = w.ModifiedOn
                };

                if (w.Id > 0)
                {
                    _workRepo.Update(work);
                    wsr.Type = "Update";
                }
                else
                {
                    _workRepo.Add(work);
                    wsr.Type = "Update";
                }
                _uow.Save();

                wsr.Success = true;
                wsr.Message = "Successfully Added";
            }
        }
        catch (Exception e)
        {
            wsr.Message = e.Message;
            wsr.Success = false;
        }

        return wsr;
    }

    public WebServiceResult MobileLogin(string user, string password)
    {
        WebServiceResult wsr = new WebServiceResult()
        {
            Action = "Mobile Login",
            Success = false,
            Type = "Mobile Login"
        };

        var strRoles = new string[] { };
        var strError = string.Empty;

        wsr.Success = (new LDAPAuth(ConfigurationManager.AppSettings["domain"]).MobileLogin(user, password,
            ref strRoles, ref strError));

        if (!wsr.Success)
        {
            wsr.Message = strError;
        }
        return wsr;
    }
}

web.config :(这给我带来了很多麻烦!!)

<system.serviceModel>

<services>
  <service name="FMS.WebServices.WorkSvc" behaviorConfiguration="Behaviour1">

    <endpoint address="http://localhost:65166/WebServices/WorkSvc.svc" contract="FMS.WebServices.IWorkSvc" binding="basicHttpBinding" /> 
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

  </service>
</services>

<behaviors>
  <serviceBehaviors>
    <behavior name="Behaviour1">
      <serviceMetadata httpGetEnabled="True" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<bindings>
  <wsHttpBinding>
    <binding name="wsHttpEndpointBinding" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647" receiveTimeout="24:0:0" sendTimeout="24:0:0" openTimeout="24:0:0">
      <security mode="None">
        <transport clientCredentialType="None" />
        <message establishSecurityContext="false" />
      </security>
    </binding>
  </wsHttpBinding>
</bindings>

0 个答案:

没有答案