mardi 4 août 2015

AutoMapper: Map null source object to decimal

I need to map an object of type PriceValue to a decimal value and I have the following mapping configured:

public class PriceValue
{
    public decimal Value { get; set; }
    ...
}

...

Mapper.CreateMap<PriceValue, decimal>()
    .ConvertUsing(src => src.Value);

The problem is when src is null an exception is thrown.

What's the best way to configure the mapping so a default(decimal) is returned instead?



via Chebli Mohamed

Select data from 3 tables with Linq to SQL

I have 3 tables. Orders, OrderItems and OrderItemServices. Each order can contain multiple OrderItems and each OrderItem can contain multiple OrderItemServices. I want to get a list of data of all orders from these tables in Linq. I could write a join but how do I make an anonymous data type to in select clasue which can give me Order list in this hierarchy?

If I use navigation properties and then select OrderItemServices in side select clause shown below it would fire individual select query for each OrderItemService which I want to avoid.

from order in Orders
select new
{
    ActiveOrders = order,
    ActiveOrderItems =order.OrderItems,
    ActiveServices = order.OrderItems.Select(o => o.OrderItemServices)
}

Is it possible to group each order with a structure of multiple items inside it and multiple services inside items?



via Chebli Mohamed

Internal Webservice IIS .NET

We have two sites on one IIS 7.5 server. Site B needs to talk to Site A through a SOAP service (Site B used to be external hence using SOAP, but now they are on the same server).

How can I reference Site A in Site B's web config without having a roundtrip through the absolute path http://ift.tt/1Dqg84B?

Essentially I want

      <endpoint address="foolocalhost/webservice/endpoint.asmx"
    binding="basicHttpBinding" bindingConfiguration="WebServiceSoap"
    contract="Reference.WebServiceSoap" name="WebServiceSoap" />

Rather than

      <endpoint address="http://ift.tt/1Dqg84B"
    binding="basicHttpBinding" bindingConfiguration="WebServiceSoap"
    contract="Reference.WebServiceSoap" name="WebServiceSoap" />

Would I need to change my host file to set foolocalhost up as a binding? Or is there an easier way around it using just the IIS GUI?



via Chebli Mohamed

Azure Web App switches server automatically?

We have a website running in azure as a Web App. Only 1 instance of the site is running. We are seeing regular downtimes for the site, and wondering why.

We use Elmah to log exceptions, and before a downtime occurs we can't see any relevant or critical errors. What we do see however, is that AFTER the downtime, the site is back up but on another server. (We can see this because the hostname of the server changes, and when an exception is thrown we can see in Elmah from which server it originated.) Small example:

Server: X
Some small Exception thrown on server X, shows up in Elmah
site goes down
Site comes back up
Server: Y
Some small exception thrown on server Y, shows up in Elmah

Now I know that azure has Rapid Fail Protection and Auto Healing, but in our scenario, I cant tell the difference between our site

a) moving to another server and going down (because there is only 1 instance)

b) rapid fail protection or auto healing kicking in

If it's option a: does anyone know based on what azure moves sites?

if it's option b: does anyone know how I can determine why/how something triggered a rapid fail protection or auto healing kick in?



via Chebli Mohamed

Given a string with day of week, return integer

Is there a way to have something like:

string day = "Sunday";
int num = getDayOfWeek(day); //returns 0

I understand we could something like, and I wanted the reverse:

int num = 0;

//returns "Sunday"
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames[(int)i] 

The easiest way would probably be to implement a dictionary that does what I am asking, but I wonder if there is something in C# DateTime functions that already does so for me.



via Chebli Mohamed

Implementing Tracing in VB .Net App

This is my fist attempt to get tracing up and running in a multi-class application. The examples I've followed and had success with aren't translating to the app I need to trace. I've read up on SO, MSDN and elsewhere, still missing something. Here's a snip of the code that's not generating anything in logs:

Imports System.Windows.Forms
Imports Inventor
Imports System.Runtime.InteropServices
Imports tankBuilder.MainForm
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System

Namespace tankBuilder
    <ProgIdAttribute("singleCourseMasterTank.StandardAddInServer"), _
    GuidAttribute("70f8dedc-4ae1-4bb9-be6b-c275afcd1333")> _
    Public Class StandardAddInServer
    Implements Inventor.ApplicationAddInServer
    Friend Shared PI As Double = 3.14159
    Friend Shared Tracer As New TraceSource("TraceSource1")
    ... logic...
Tracer.TraceInformation("Informational message.")
Tracer.TraceEvent(TraceEventType.Error, 1, "Error message.")
Tracer.TraceEvent(TraceEventType.Warning, 2, "Warning message.")

Here's the corresponding section from app.config

<system.diagnostics>
   <sources>
     <source name="TraceSource1"
       switchName="sourceSwitch"
       switchType="System.Diagnostics.SourceSwitch">
       <listeners>
         <add name="console"
           type="System.Diagnostics.ConsoleTraceListener">
           <filter type="System.Diagnostics.EventTypeFilter"
             initializeData="Error"/>
         </add>
         <add name="prodListener"
           type="System.Diagnostics.TextWriterTraceListener"
           initializeData="C:\Data\Projects\trace.log">
         </add>
         <remove name="Default"/>
       </listeners>
     </source>
   </sources>
   <switches>
     <add name="sourceSwitch" value="4"/>
   </switches>
</system.diagnostics>

Thanks so much in advance!



via Chebli Mohamed

Modifying a list from another form

I am trying to set up a list in C# and I want it so that when I press the an add button it will open a window with fields where I can enter data press and ok button and then have those text box fields transferred into the list. This is my code for the popup form.

public partial class addtoLibraryDialog : Form
{
    public addtoLibraryDialog()
    {
        InitializeComponent();
    }


    private void btnOK_Click(object sender, EventArgs e)
    {
        ListViewItem list = new ListViewItem("name");
        list.SubItems.Add("path");
        listView1.Items.Add(list);
    }
}

And I was wondering how I can make it so the listview1 is recognised. I have found information on this outline but as I am new to programming I can't really make heads of tails of them.



via Chebli Mohamed